Get-Service in PowerShell: List Running Services and More

Get-Service in PowerShell: list running services and filter them

Get-Service is the PowerShell cmdlet that reads the Windows services on your PC. On its own it lists every service; pipe it to Where-Object to list only the running ones. Here I check a set of standard Windows services: Output: Without -Name, Get-Service returns every service on the PC. I kept the examples to built-in … Read more

Get the Current Directory and Script Path in PowerShell ($PSScriptRoot)

Get the current directory and script path in PowerShell

Get-Location (or $PWD) tells you the current directory in PowerShell. For the PowerShell script path, read $PSScriptRoot for the script’s folder or $PSCommandPath for the full .ps1 path: Those two answers differ more often than you’d think. The current directory is wherever the caller happens to be, while the script path never moves. I ran … Read more

Echo in PowerShell: How Write-Output Works (With Examples)

Echo in PowerShell and Write-Output cmdlet examples

You type echo in PowerShell out of CMD or Bash habit, and it mostly works. Then a sentence splits across three lines, or echo -n does nothing you expected. Quick answer: echo is a built-in alias for Write-Output. It sends objects to the success stream (the pipeline). If nothing consumes them, they’re shown in the … Read more

PowerShell Escape Character (Backtick): Escape Special Characters

PowerShell escape character (backtick) examples

The PowerShell escape character is the backtick (`), the key just below Esc on a US keyboard. Inside double quotes, a backtick makes the next character literal, like `$, or turns it into a special character, like `t for a tab: Output: A backslash is just a normal character in PowerShell, so Windows paths need … Read more

Create a Directory in PowerShell with New-Item (and mkdir)

Create a directory in PowerShell with New-Item

Creating a directory in PowerShell takes one command: New-Item -Path C:\Reports -ItemType Directory. Change the item type to File and the same cmdlet creates a file. Here are both in one script: Output: New-Item hands back the new item, a DirectoryInfo for a folder and a 0-byte FileInfo for a file, so you can use … Read more

PowerShell Do-While, While and Do-Until Loops (With Examples)

PowerShell do-while, while and do-until loops

A do-while loop in PowerShell runs its block once, then keeps repeating it as long as the condition after while is true. Because the check comes at the end, the block always runs at least one time: Output: A plain while loop checks its condition first instead, so its block may never run. Below I’ll … Read more

PowerShell Hashtable (Dictionary): Create, Add, Loop and Sort

PowerShell hashtable created, filled in a loop and sorted

Think of a hashtable in PowerShell as a lookup list: each value sits under a key, and you get it back by that key instead of by position. Create one with @{ Key = ‘Value’ } and read it with $table[‘Key’]: Output: A hashtable is PowerShell’s built-in dictionary, so you’ll also see it called a … Read more

PowerShell -in, -notin and -contains Operators (With Examples)

PowerShell -in and -contains operator examples

The PowerShell -in operator checks whether a value is in a collection, and -contains runs the same check with the collection written first. Both return True or False: They compare whole items, never parts of text, and that trips up a lot of scripts. I ran each example below in PowerShell 7 and Windows PowerShell … Read more

How to Convert Variables to Strings in PowerShell

Convert a variable to a string in PowerShell

To convert a variable to a string in PowerShell, cast it with [string], call its ToString() method, or put it inside double quotes. For a number, all three give the same text: Output: They don’t always agree, though. Dates, decimals, arrays and $null each behave a little differently depending on which one you use, and … Read more

How to Check if a String Contains Only Numbers in PowerShell?

powershell check if string contains only numbers

One of my team members was searching for a PowerShell script to check if a string contained only numeric characters. I suggested a few methods. In this tutorial, I will explain how to check if a string contains only numbers in PowerShell using various methods. To check if a string contains only numbers in PowerShell, … Read more