PowerShell Where-Object Contains (With Examples)

PowerShell Where-Object contains examples

To filter objects where a property contains some text, use Where-Object with the -like operator and wildcards on both sides. This returns every server whose name contains “SQL”: The -contains operator looks like the obvious choice, but it doesn’t check for text inside a string. That trips up almost everyone, so I’ll show you why … Read more

PowerShell Naming Conventions: Functions, Parameters and Variables

PowerShell naming conventions

PowerShell naming conventions come down to one pattern for commands: an approved verb, a hyphen, and a singular noun, both in PascalCase. Parameters use PascalCase too. Here’s a function that follows all of it: Output: Anyone who knows Get-Service or Get-ChildItem can guess what Get-DiskReport does and how to call it. That’s the whole point … Read more

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 Check the PowerShell Version (Windows, Scripts and Remote)

How to check the PowerShell version

To check the PowerShell version, run $PSVersionTable.PSVersion. It works in every version on Windows, macOS and Linux. Here it is in PowerShell 7: Output in PowerShell 7.6: The same command in Windows PowerShell 5.1 shows a different layout, with Build and Revision instead of Patch: Most Windows 11 PCs have both: Windows PowerShell 5.1 is … 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 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