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 Write-Host vs Write-Output (and echo, Verbose, Error)

PowerShell Write-Host vs Write-Output

PowerShell Write-Host vs Write-Output comes down to where the text goes. Write-Output sends data down the pipeline, so you can save it or pass it on, while Write-Host only displays text on the screen: Output: Below I compare both with echo, Out-Host, Write-Information, Write-Verbose and Write-Error. Every example ran in PowerShell 7.6 and Windows PowerShell … 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 Find the Most Recent File in a Directory with PowerShell

Find the most recent file in a folder with PowerShell

To find the most recent file in a directory with PowerShell, list the files with Get-ChildItem, sort them by LastWriteTime from newest to oldest, and keep the first one: Output: LastWriteTime is the date Windows shows as Date modified. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 against the same sample folder, … Read more

How to Create Desktop Shortcuts with Custom Icons Using PowerShell

Create desktop shortcuts with custom icons using PowerShell

To create a desktop shortcut with a custom icon in PowerShell, use the WScript.Shell COM object. CreateShortcut makes a .lnk file, and you set its TargetPath, IconLocation and other properties before calling Save(): Output: I created every shortcut in a test folder so the output is easy to check. The last section shows how to … Read more

How to Add a Date to a File Name Using PowerShell

Add date to file name in PowerShell

To add a date to a filename in PowerShell, format the date as text with (Get-Date).ToString(‘yyyy-MM-dd’) or Get-Date -Format ‘yyyy-MM-dd’, then put it inside the name: Output: I used a fixed date so the output doesn’t change. In a real script, use $now = Get-Date. Every example here ran in PowerShell 7.6 and Windows PowerShell … Read more

PowerShell Arithmetic Operators: +, -, *, /, %, ++ and More

PowerShell arithmetic operators examples

PowerShell arithmetic operators are + for addition, – for subtraction, * for multiplication, / for division and % for the remainder (modulus). They work on numbers the way you’d expect: Output: They also have a few PowerShell-specific habits, like division that returns decimals and + that joins strings. I ran every example in PowerShell 7.6 … Read more

How to Convert a String to a Boolean in PowerShell (and Boolean to Int)

Convert string to Boolean in PowerShell

To convert a string to a Boolean in PowerShell, use [System.Convert]::ToBoolean(). It turns the text True or False into the matching Boolean value, ignoring case: Output: Don’t cast with [bool] for this. [bool]’False’ returns True, which I show next. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results. Why … Read more

How to Clear, Reset and Remove Variables in PowerShell

Clear variables in PowerShell

To clear variables in PowerShell, use Clear-Variable to empty a variable’s value, or Remove-Variable to delete the variable completely. Both take the name without the $ sign: Output: Below I compare these with setting a variable to $null, then cover typed variables, collections, read-only variables, scopes and removing every variable a script created. I ran … Read more