PowerShell Select-Object: Properties, -First, -Unique and More

PowerShell Select-Object examples

Select-Object is the PowerShell cmdlet that picks properties from objects, or picks a number of objects from a list. The examples below use this list of employees: To keep only some properties, list them after -Property: Output: I ran every example in real PowerShell 7.6 and Windows PowerShell 5.1 consoles. Where a parameter exists in … Read more

How to Prompt for Input in PowerShell (Read-Host Examples)

Prompt for user input in PowerShell with Read-Host

To prompt for input in PowerShell, use Read-Host. It shows your message, waits until the user types something and presses Enter, and returns what they typed: Output (what I typed follows the colon): PowerShell adds the colon and space after your prompt text, so leave them out of the message. The value goes into $name, … Read more

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 Add Days to a Date in PowerShell (and Months, Yesterday)

Add days to a date in PowerShell

To add days to a date in PowerShell, call the date’s AddDays() method. Use a negative number to go back in time, like AddDays(-7) for a week ago: Output: For today’s date, start from (Get-Date), as in (Get-Date).AddDays(30). I used fixed dates so you get the same output, and I ran every example in PowerShell … Read more

PowerShell Pipeline Variables [With Examples]

PowerShell Pipeline Variables

Recently, I gave an online session on pipeline variables for a PowerShell user group. In this tutorial, I will explain everything about PowerShell pipeline variables with examples. Pipeline variables in PowerShell, introduced with the -PipelineVariable parameter in PowerShell 7, allow you to store the output of a command within a pipeline for reuse later in the same … Read more

How to Increment a Variable in PowerShell (Numbers and Strings)

Increment a variable in PowerShell

To increment a variable in PowerShell, use the ++ operator, like $count++, to add 1. Use += to add any other amount, and — to subtract 1: Output: That covers numbers. Strings, IDs with leading zeros and variables inside functions need a little more care, which I show below. Every example ran in PowerShell 7.6 … Read more

PowerShell Random Password Generator

powershell generate random password

Recently, one of my clients asked me to create a random password generator. I suggested PowerShell for it. In this tutorial, I will show you how to create a random password generator using PowerShell. Generate Random Password Using PowerShell Now, let me show you different methods to generate random passwords using PowerShell. Method 1: Using … Read more

How to Generate Random Strings in PowerShell

Generate random strings in PowerShell

To generate a random string in PowerShell, turn a list of allowed characters into an array, pick characters with Get-Random in a loop, and join them with -join: Output (yours will differ): That’s fine for test data and file names. For passwords, API keys or tokens, use the secure method further down, since Get-Random isn’t … Read more

How to Extract Text Between Delimiters in PowerShell

Extract text between delimiters in PowerShell

To extract text between two delimiters in PowerShell, match it with a regular expression and a capture group. The part in parentheses lands in $Matches[1]: Output: The ? after .* matters more than it looks, and -match only returns the first match. Both are covered below. I ran every example in PowerShell 7.6 and Windows … Read more