How to Get Computer Name in PowerShell?

Get Computer Name in PowerShell

Recently, someone asked me to get the computer’s name in PowerShell. I tried different methods. In this tutorial, I will show you how to get the computer name in PowerShell using various cmdlets. To get the computer name in PowerShell, you can use several methods. The simplest is using the environment variable $env:COMPUTERNAME. Alternatively, you … 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 Generate Random Numbers in PowerShell?

powershell random number within range

Recently, someone asked me about different methods to generate random numbers in PowerShell. I suggested different methods with examples. In this tutorial, I will show you how to generate random numbers in PowerShell with examples. To generate random numbers in PowerShell, you can use the Get-Random cmdlet. For example, to generate a single random number, … 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

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 Create Variables with Multiple Values in PowerShell?

powershell create variable with multiple values

Recently, one of my team members got a requirement from the client to create a variable with multiple values in PowerShell. I suggested a few useful methods. In this tutorial, I will explain how to create variables with multiple values in PowerShell with examples. To create a variable with multiple values in PowerShell, use an … Read more