How to Remove Environment Variables in PowerShell

Remove environment variables in PowerShell

To remove an environment variable in PowerShell for the current session, run Remove-Item on the Env: drive: That only affects the current window. To delete a variable permanently, use the .NET method with the User or Machine scope, which I cover below. I also show a change in PowerShell 7.5 that trips up older scripts. … 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

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

How to Get the Difference Between Two Dates in PowerShell

Get the difference between two dates in PowerShell

To get the difference between two dates in PowerShell, subtract one [datetime] from the other. The result is a TimeSpan, and its TotalDays, TotalHours and TotalMinutes properties give the full difference in each unit: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results. All dates are fixed so … Read more

PowerShell Date Comparison: Compare Dates the Right Way

PowerShell date comparison

To compare dates in PowerShell, make sure both sides are [datetime] values, then use -lt, -gt and -eq. Subtracting one date from another tells you how far apart they are: Output: Earlier means smaller, so -lt reads as “before” and -gt as “after.” I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … 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

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 an HTML Table in PowerShell

Create an HTML table in PowerShell

To create an HTML table in PowerShell, pipe objects to ConvertTo-Html. Use -Property to pick the columns and -Fragment to get just the table: Output: Each object becomes a row and each property a column. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and point out where the output differs. Arrays of … Read more