How to Check if a String Contains a Substring in PowerShell

Check if a string contains a substring in PowerShell

To check if a string contains a substring in PowerShell, use the -like operator with a * wildcard on each side. It returns True or False and ignores case: Output: Use -clike when case matters. For most checks, like looking for a server name or an error word in a log line, the case-insensitive -like … Read more

PowerShell For Loop (With Examples)

PowerShell for loop with examples

A PowerShell for loop runs a block of code a set number of times. You give it a starting value, a condition and a step, and PowerShell repeats the block while the condition is true: Here’s the simplest real example, counting from 1 to 5: Output: Below, you’ll see how each part works, how to … Read more

PowerShell Switch Statement (With Examples)

PowerShell switch statement with examples

The PowerShell switch statement compares a value against a list of conditions and runs the script block of every condition that matches. It’s a cleaner replacement for a long chain of if/elseif statements. Here’s the basic form, with a default block for anything that doesn’t match: Output: In this guide, I’ll cover multiple conditions, greater … Read more

PowerShell Filter Operators (With Examples)

PowerShell filter operators with examples

PowerShell filter operators are the comparison operators, like -eq, -gt, -like, -match and -in, used to keep only the items you want. Put them inside Where-Object, or apply them straight to an array: Output: When the left side is an array, the operator returns the matching items instead of True or False. Below I cover … 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

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

PowerShell IsNullOrEmpty: Check if a Variable Is Null or Empty

PowerShell IsNullOrEmpty and null checks

To check if a string is null or empty in PowerShell, use [string]::IsNullOrEmpty($value). It returns True for $null and for an empty string, and False for anything else: Output: That covers most scripts. Below I compare it with the other null checks, since each one treats values like spaces and 0 differently. I ran every … Read more

How to Save Output to a File in PowerShell

Save output to a file in PowerShell

To save output to a file in PowerShell, pipe it to Out-File, or use the > redirection operator, which does the same thing: Both save the output exactly as it looks on screen. Below I compare every option with the same three sample orders, so you can see which file each one produces. Which method … Read more

How to Get the File Name from a Path in PowerShell

Get file name from path in PowerShell

To get the file name from a path in PowerShell, use Split-Path -Leaf. -Parent gives you the folder part instead: Output: Split-Path only works on the text, so the file doesn’t need to exist. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note the 7-only options. Get the name without the … Read more

How to Find a File by Name in PowerShell

Find a file by name in PowerShell

To find a file by name in PowerShell, use Get-ChildItem with -Filter for the name and -Recurse to search every subfolder. This finds every file called notes.txt under a projects folder: Output: FullName gives you the complete path of each match. Name matching isn’t case-sensitive, so NOTES.TXT would find the same files. My test folder … Read more