How to Create a Table with Headers in PowerShell

Create a table with headers in PowerShell

To create a table with headers in PowerShell, make [pscustomobject] rows. The property names become the column headers, and PowerShell prints the rows as a table: Output: For custom header text, widths and number formats, use Format-Table with calculated columns. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and the tables match … Read more

How to Create Objects in PowerShell (Custom Objects and Classes)

Create objects in PowerShell

To create an object in PowerShell, cast a hashtable to [pscustomobject]. The keys become properties, in the order you wrote them: Output: That covers most scripts. Below I add methods and computed properties, use classes for typed objects, and turn strings into objects. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. Add … 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

ConvertTo-Json in PowerShell (With Examples)

PowerShell ConvertTo-Json

ConvertTo-Json turns PowerShell objects, hashtables and arrays into JSON text. Pipe the data to it, and use [ordered] so the keys keep your order: Output in PowerShell 7: Output in Windows PowerShell 5.1: Both are valid JSON with the same data, only the spacing differs. I ran every example in PowerShell 7.6 and Windows PowerShell … Read more

How to Create Files with Content Using PowerShell (Text, JSON, XML)

Create files with content using PowerShell

To create a file with content in PowerShell, use Set-Content -Path file -Value ‘text’. It creates the file, or replaces its content if the file already exists: Output: Below I cover multiline text, encoding, and JSON and XML files. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and show where they write … Read more

Measure-Object in PowerShell: Sum, Average, Max and Count

Measure-Object in PowerShell

Measure-Object calculates the count, sum, average, maximum and minimum of numbers or of a property. Pipe the values to it and pick the statistics you want: Output in Windows PowerShell 5.1: PowerShell 7 shows the same numbers plus an empty StandardDeviation row. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note … Read more

PowerShell Multidimensional Arrays (Jagged and 2D)

PowerShell multidimensional arrays

A multidimensional array in PowerShell is usually an array of arrays, called a jagged array. Each inner array is a row, and you read a value with two indexes, $array[row][column]: Output: PowerShell also has true two-dimensional arrays like [int[,]], covered further down. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the … 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

PowerShell Not Equal Operator (-ne) With Examples

PowerShell not equal operator

The not equal operator in PowerShell is -ne. It returns True when two values are different, and it ignores case for text: Output: PowerShell doesn’t support !=. Writing if (5 != 3) fails with “Unexpected token ‘!=’ in expression or statement.” I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same … Read more

How to Check if a File Contains a String in PowerShell

Check if a file contains a string in PowerShell

To check if a file contains a string in PowerShell, run Select-String with -Quiet. It returns True or False, so it fits straight into an if statement: Output: The match ignores case, and the pattern is a regular expression. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 on a sample Logs folder, … Read more