How to Convert a String to an Integer in PowerShell (and Decimal, Double)

Convert string to int in PowerShell

To convert a string to an integer in PowerShell, cast it with [int]. After that, math works as numbers instead of text: Output: Use [decimal] for money and [double] for other fractions. Below I cover safe conversion, rounding, commas, characters and scientific notation. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with … Read more

PowerShell Comparison Operators: -eq, -ne, -gt, -ge, -lt, -le

PowerShell comparison operators

PowerShell’s comparison operators are -eq (equal), -ne (not equal), -gt (greater than), -ge (greater than or equal), -lt (less than) and -le (less than or equal). Each returns True or False: Output: PowerShell uses these letter operators instead of =, > and <, which mean something else here. I ran every example in PowerShell 7.6 … Read more

How to Remove Characters from Strings in PowerShell

Remove characters from strings in PowerShell

To remove characters from a string in PowerShell, replace them with nothing. Use .Replace() for exact characters, or -replace with a pattern to remove a whole group at once: Output: The second line keeps only the digits, which is the easiest way to clean up a US phone number. I ran every example in PowerShell … Read more

How to Convert a String to Base64 in PowerShell (and Back)

Convert a string to Base64 in PowerShell

To convert a string to Base64 in PowerShell, turn the text into bytes with [System.Text.Encoding]::UTF8.GetBytes(), then pass the bytes to [System.Convert]::ToBase64String(): Output: To decode Base64 back to text, reverse the two steps. FromBase64String() returns the bytes, and GetString() turns them into text: Output: I ran every example in PowerShell 7.6 and Windows PowerShell 5.1. The … Read more

How to Convert a String to a Date in PowerShell

Convert a string to a date in PowerShell

To convert a string to a date in PowerShell, cast it with [datetime] when it’s in US or ISO format. For any other layout, use [datetime]::ParseExact() and tell it the exact format: Output: Once it’s a DateTime, you can format it, compare it and do date math with it. I ran every example in PowerShell … Read more

How to Change Directory in PowerShell (cd and Set-Location)

Change directory in PowerShell

To change directory in PowerShell, use Set-Location, or its short alias cd, followed by the folder path. Get-Location shows where you are now: Output: Below I cover relative paths, folders with spaces, going back to the previous folder, and a .NET gotcha that trips up scripts. Every example ran in PowerShell 7.6 and Windows PowerShell … Read more

How to Set and Get Environment Variables in PowerShell

Set and get environment variables in PowerShell

To set an environment variable in PowerShell, assign it with $env:, and to get it, read $env: the same way. This lasts until you close the window: Output: To keep a variable after you close PowerShell, save it to the User or Machine scope with [Environment]::SetEnvironmentVariable. Below I cover every way to set, get, persist … 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 Set Variables in PowerShell

Set variables in PowerShell

To set a variable in PowerShell, type a dollar sign, the name, an equals sign and the value, like $state = ‘Texas’. PowerShell creates the variable the first time you assign it: Output: That’s how you’ll set almost every variable. The cmdlets New-Variable and Set-Variable are for special cases, which I cover below. I ran … Read more

PowerShell Random Password Generator (Secure Methods)

PowerShell random password generator

To generate a random password in PowerShell 7.4 or later, pick characters with Get-SecureRandom, which uses a cryptographically secure generator, and join them into a string: For Windows PowerShell 5.1, or when you must guarantee every character type, use the New-RandomPassword function below. Every password in this post is a fresh random sample, so yours … Read more