How to Create an Empty Array in PowerShell

Create an empty array in PowerShell

To create an empty array in PowerShell, assign @() to a variable. You can then add items with +=, and Count tells you how many it holds: Output: That’s fine for small lists. Below I cover typed and fixed-size arrays, a faster List, and the checks that trip people up. I ran every example in … Read more

How to Sort an Array in PowerShell (Strings, Numbers, Objects)

Sort an array in PowerShell

To sort an array in PowerShell, pipe it to Sort-Object. It sorts in ascending order, A to Z and smallest to largest, and ignores case. Add -Descending to reverse it: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Most results match, but a few sorts differ between the versions, and I … Read more

PowerShell Append to Array: +=, Lists and Common Mistakes

Append to an array in PowerShell

To append to an array in PowerShell, use the += operator. It adds the new item to the end, so a list of two servers becomes three: Output: That’s all you need for small scripts. But += doesn’t really grow the array. It builds a brand-new one each time, which explains most of the surprises … Read more

How to Remove Duplicates from an Array in PowerShell

Remove duplicates from an array in PowerShell

To remove duplicates from an array in PowerShell, pipe it to Select-Object -Unique. It keeps the first copy of each value, in the original order: Output: Notice that Austin and austin both stayed. Select-Object -Unique is case-sensitive, and each method treats case and order differently. I ran every example in PowerShell 7.6 and Windows PowerShell … Read more

How to Create an Array of Objects in PowerShell

Create an array of objects in PowerShell

To create an array of objects in PowerShell, put [pscustomobject]@{ … } entries inside @( ), one per line. Each object’s keys become its properties: Output: PowerShell shows the array as a table, with one row per object. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note the two places they … Read more

How to Compare Two Arrays in PowerShell (Matches and Differences)

Compare two arrays in PowerShell

To compare two arrays in PowerShell, use Compare-Object. It lists the items that are only in one array: <= means only in the first (reference) array, and => only in the second: Output: WEB02 and web02 matched, because Compare-Object ignores case and order. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

How to Remove Items from an Array in PowerShell

Remove items from an array in PowerShell

To remove items from an array in PowerShell, build a new array without them and assign it back. Where-Object does this in one line: Output: PowerShell arrays have a fixed size, so you can’t delete an item in place. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note the one 7-only … Read more

How to Create a Character Array in PowerShell

Create a character array in PowerShell

To create a character array in PowerShell, call ToCharArray() on a string. It returns a Char[] array with one item for each character, which you can loop through, filter or count: Output: Below I compare the other ways to build one, then cover letter ranges, filtering, counting and joining characters back into a string. Every … Read more

How to Replace Values in an Array in PowerShell

Replace values in an array in PowerShell

To replace a value in a PowerShell array, assign to its index: $array[1] = ‘new value’. To replace text in every item at once, use -replace on the whole array: Output: Index assignment changes the array in place. The rest of this guide covers replacing across all items, replacing whole items only, and a few … Read more