PowerShell Where-Object Contains (With Examples)

PowerShell Where-Object contains examples

To filter objects where a property contains some text, use Where-Object with the -like operator and wildcards on both sides. This returns every server whose name contains “SQL”: The -contains operator looks like the obvious choice, but it doesn’t check for text inside a string. That trips up almost everyone, so I’ll show you why … 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

How to List Directories and Files in PowerShell

List directories and files in PowerShell

To list directories and files in PowerShell, use Get-ChildItem. On its own, it lists every folder and file in the current location. Add -Directory for folders only, -File for files only and -Recurse to include subfolders. Here it is on a sample Projects folder I created for this article: Output: The Mode column tells you … 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

How to Check Hard Drive Space Using PowerShell

Check hard drive space using PowerShell

To check hard drive space using PowerShell, run Get-Volume. It shows the free space (SizeRemaining) and total size of each drive, and it works without admin rights in Windows PowerShell 5.1 and PowerShell 7: Output: For a report you can reuse, including free space in GB, percent free and remote servers, query Win32_LogicalDisk. I’ll build … Read more

How to Show Logged-In Users with PowerShell

Show logged-in users with PowerShell

To show logged-in users with PowerShell, run quser (short for query user). It lists every user signed in to the computer, including Remote Desktop sessions, with their session ID, state, idle time and logon time. Two other commands answer slightly different questions: Command What it returns Remote PCs quser Every signed-in user and session, active … Read more

How to Get a List of Installed Programs Using PowerShell

Get a list of installed programs using PowerShell

To get a list of installed programs using PowerShell, read the Uninstall keys in the registry with Get-ItemProperty. That’s the same data Windows shows in Settings > Apps > Installed apps, and it works without admin rights. Here’s the short version. It checks the 64-bit, 32-bit and per-user locations and prints each program’s name, version … Read more