How to Convert pwdLastSet to a Date in PowerShell

Convert pwdLastSet to a date in PowerShell

To convert pwdLastSet to a date in PowerShell, pass the number to [datetime]::FromFileTimeUtc(). pwdLastSet is a Windows FILETIME, the count of 100-nanosecond ticks since January 1, 1601 in UTC: Below I convert it to Eastern time, handle the special 0 value, turn a date back into a FILETIME for LDAP filters and build a password … Read more

How to Read a JSON File into an Array in PowerShell

Reading a JSON file into a PowerShell array with ConvertFrom-Json

To read a JSON file into an array in PowerShell, read the whole file as one string with Get-Content -Raw and pipe it to ConvertFrom-Json. A JSON array becomes a PowerShell array of objects: That one line covers most files. The rest of this guide handles the cases that break it: one-item arrays, nested arrays, … Read more

PowerShell Where-Object vs Select-Object: Rows vs Properties

PowerShell Where-Object vs Select-Object

The short version of Where-Object vs Select-Object: Where-Object decides which objects (rows) stay in the pipeline, and Select-Object decides which properties (columns) each object keeps. Most real commands use both, filter first: I’ll show what each cmdlet returns, why the order matters, the row tricks Select-Object also has, and one version difference between PowerShell 7 … Read more

How to Expand an Array in PowerShell (-ExpandProperty and More)

How to expand an array in PowerShell

To expand an array in PowerShell, pipe the objects to Select-Object -ExpandProperty and name the property that holds the array. You get every value as its own item instead of a {a, b} blob: Below I cover dot notation, keeping the parent value, the {a, b, c…} display limit, flattening nested arrays and growing an … Read more

How to Convert UTF-8 to ANSI Using PowerShell

Convert UTF-8 to ANSI using PowerShell

To convert a UTF-8 file to ANSI in PowerShell 7.4 or later, read it as UTF-8 and write it back with -Encoding ansi: Windows PowerShell 5.1 has no ansi value and uses -Encoding Default instead, which means something completely different in PowerShell 7. Below I check the real bytes in both versions and show a … Read more

How to Reverse an Array in PowerShell (In Place or as a Copy)

How to reverse an array in PowerShell

To reverse an array in PowerShell, pass it to the .NET [array]::Reverse() method. It flips the items in place, so the same variable now holds them in the opposite order: To keep the original order too, a negative index range gives you a reversed copy. I ran each method in PowerShell 7 and 5.1. Reverse … Read more

How to Clear an Array in PowerShell (@(), Clear(), ArrayList)

How to clear an array in PowerShell

The quickest way to clear an array in PowerShell is to assign it a new empty array with @(). The variable then holds zero items: That isn’t the only option, and the obvious one, $cities.Clear(), doesn’t do what most people expect. I tested each method in PowerShell 7 and 5.1, and one works only in … Read more

How to Remove Blank Lines from an Array in PowerShell

How to remove blank lines from an array in PowerShell

To remove blank lines from an array in PowerShell, pipe the array to Where-Object and keep only items that aren’t null, empty or whitespace: I tested every example below in PowerShell 7 and Windows PowerShell 5.1, including a popular shortcut that crashes on null items. Remove blank lines from an array with Where-Object This array … Read more