How to Get the First and Last Element of an Array in PowerShell

Get the first and last element of an array in PowerShell

To get the first element of an array in PowerShell, use index 0: $array[0]. For the last element, use -1: $array[-1]. Negative numbers count back from the end: Output: Below I cover getting several items at once, Select-Object, and the cases where these give surprising results. I ran every example in PowerShell 7.6 and Windows … 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 Write to a Log File in PowerShell

PowerShell write to log file

To write to a log file in PowerShell, use Add-Content with a timestamp from Get-Date. Add-Content creates the file the first time and appends a new line on every call after that: Output: Below I turn this into a reusable Write-Log function, add daily log files and error logging, and show one file-name mistake that … Read more

PowerShell Logical Operators: -and, -or, -not, -xor and !

PowerShell logical operators

PowerShell’s logical operators are -and, -or, -xor, -not and !. They combine or flip True/False conditions, mostly inside if statements and Where-Object filters: Output: ! is just a shorter spelling of -not. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results except the last section. What each logical operator … Read more

How to Trim Strings in PowerShell

Trim strings in PowerShell

To trim a string in PowerShell, call .Trim() to remove spaces from both ends, or .TrimStart() and .TrimEnd() for one side only: Output: Below I cover trimming specific characters, the first or last few characters, and text before or after a character. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, and I … Read more

How to Get the Type of an Object in PowerShell

Get the type of an object in PowerShell

To get the type of an object or variable in PowerShell, call its GetType() method. .Name gives the short name and .FullName the full .NET name: Output: To test for a type instead of reading it, use the -is operator, covered below. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the … Read more

PowerShell Find and Replace in File (Without Breaking It)

PowerShell find and replace in file

To find and replace text in a file with PowerShell, read the file with Get-Content in parentheses, use -replace, and write it back with Set-Content: Output (the new file content): The parentheses matter, and so do a few other details that quietly damage files. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 … Read more

PowerShell Prompt for Yes/No Input (6 Ways)

PowerShell prompt for yes/no input

To prompt for yes/no input in PowerShell, ask with Read-Host and check the answer with -in ‘y’, ‘yes’. The -in operator ignores case, so Y, y and YES all count: Output (I typed Y): That’s enough for a quick script. Below are sturdier options: a loop that rejects bad answers, a reusable function with a … 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

How to Get or Set the Default Browser Using PowerShell

Set the default browser using PowerShell

To set the default browser for users with PowerShell, create a default associations XML file for the browser and apply it with the Set a default associations configuration file policy. Windows applies it when the user signs in. You can’t just change a registry value, which is what most scripts online try. Below I show … Read more