PowerShell Where-Object: Filter Objects With Examples

PowerShell Where-Object with examples

PowerShell Where-Object filters objects in the pipeline. You give it a condition in a script block, and it passes on only the objects where the condition is true. Here it keeps orders over $900: Output: $_ is the object being checked. The other examples in this guide use the same five sample orders in $orders, … 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 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 Pick Random Items from an Array in PowerShell?

Pick Random Items from an Array in PowerShell

Sometimes, you might need to pick a random item from a PowerShell array. In this PowerShell tutorial, I will explain to you how to pick random items from an array in PowerShell. Let us explore how to use PowerShell to get random items from an array. To pick random items from an array in PowerShell, … 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

How to Check if an Array Contains a Value in PowerShell

Check if a PowerShell array contains a value

To check if an array contains a value in PowerShell, use the -contains operator. It returns True or False, and it ignores case: Output: That covers the everyday case. Below, I’ll handle the variations people actually search for: case-sensitive checks, partial matches, “does not contain”, several values at once, the index of an item, and … Read more

How to Convert Object to Array in PowerShell?

Convert Object to Array in PowerShell

Do you need to convert an object to an array in PowerShell? In this PowerShell tutorial, I will explain how to convert the object to an array in Powershell using various methods. To convert an object to an array in PowerShell, you can cast the object to an array type by prefixing it with [array], … 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 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 Join Arrays in PowerShell?

Join Arrays in PowerShell

Do you want to combine multiple arrays in PowerShell? In this PowerShell tutorial, I will explain how to join arrays in PowerShell, a common task when combining multiple arrays into a single array or a string. To join arrays in PowerShell, you can use the + operator to concatenate them. For instance, $joinedArray = $array1 + $array2 will create … Read more