PowerShell Ordered Arrays: [ordered] Hashtables and Sorted Collections

PowerShell ordered array and ordered dictionary examples

A PowerShell ordered array is usually an ordered hashtable: put [ordered] in front of @{} and the keys stay in the order you typed them. Plain arrays already keep their order, so they don’t need anything extra: Below, I’ll add, insert, read and sort entries, then cover the collections that keep items sorted. I tested … 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

How to Split an Array into Smaller Arrays in PowerShell

Split an array into smaller arrays in PowerShell

PowerShell has no built-in chunk cmdlet, so to split an array into smaller arrays you step through it with a for loop and slice each piece with a range. The comma keeps every chunk as its own array: I’ll also wrap it in a function, show the one-chunk trap, and cover LINQ Chunk, a set … Read more

How to Print an Array in PowerShell (One Line, Loop, Table)

How to print an array in PowerShell

To print an array in PowerShell, type the variable name or pass it to Write-Output, and every item prints on its own line. For a single line, join the items with -join: That covers most cases, but arrays of objects, hashtables and files behave differently. I ran each example below in PowerShell 7 and Windows … Read more

How to Find Invalid Characters in a String Using PowerShell

Find invalid characters in a string using PowerShell

To find invalid characters in a string in PowerShell, compare it against the characters Windows doesn’t allow. For file names, IndexOfAny with GetInvalidFileNameChars returns the position of the first bad one, or -1 if the name is clean: “Invalid” depends on where the text is going, though. Below I check file names, list every bad … Read more