You do not need long scripts to be productive in PowerShell. These ten one-liners answer the questions Windows administrators ask every day. Open PowerShell as Administrator and try them.
How a one-liner works: objects, not text
Linux shells pass plain text from one command to the next. PowerShell passes objects: every row is an object with named properties (a service has Name, Status and StartType, a file has Length and LastWriteTime). That is why one-liners stay short, because you filter and sort by property name instead of cutting text apart.
Where-Objectkeeps only the rows you want.Select-Objectchooses columns, or the first N rows with-First.Sort-Objectorders the rows.$_means “the current row” inside{ }blocks.@{n='Name';e={ ... }}adds a calculated column:nis the column name andeis the expression that produces the value.Format-TableandFormat-Listonly change how the result is displayed. Always put them last in the pipeline.
1. Which services are stopped?
Get-Service | Where-Object Status -eq 'Stopped'
Add -Name 'SQL*' to Get-Service to narrow it to services whose name starts with SQL.
2. Top 10 processes by memory
Get-Process | Sort-Object WorkingSet -Descending |
Select-Object -First 10 Name, Id, @{n='MemoryMB';e={[int]($_.WorkingSet/1MB)}}
3. Free and used space on every drive
Get-PSDrive -PSProvider FileSystem |
Select-Object Name,
@{n='UsedGB';e={[math]::Round($_.Used/1GB,1)}},
@{n='FreeGB';e={[math]::Round($_.Free/1GB,1)}}
4. The ten largest files on C:
Get-ChildItem C:\ -Recurse -File -ErrorAction SilentlyContinue |
Sort-Object Length -Descending |
Select-Object -First 10 FullName, @{n='SizeMB';e={[int]($_.Length/1MB)}}
This can take a few minutes on a big disk. Point it at a specific folder to make it faster.
5. System errors from the last 24 hours
Get-WinEvent -FilterHashtable @{LogName='System'; Level=2; StartTime=(Get-Date).AddDays(-1)} -MaxEvents 20 |
Format-Table TimeCreated, ProviderName, Id, Message -Wrap
Level 2 means Error. Use Level 3 for warnings.
6. Is a remote port reachable?
Test-NetConnection -ComputerName server01 -Port 443
Look for TcpTestSucceeded : True. It is the PowerShell replacement for telnet.
7. When did this server last reboot?
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime
8. Latest installed updates
Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 5 HotFixID, Description, InstalledOn
9. Certificates that expire within 30 days
Get-ChildItem Cert:\LocalMachine\My |
Where-Object NotAfter -lt (Get-Date).AddDays(30) |
Select-Object Subject, NotAfter, Thumbprint
10. Send any result to a CSV file
Get-Service | Where-Object Status -eq 'Running' |
Select-Object Name, DisplayName, StartType |
Export-Csv C:\Temp\running-services.csv -NoTypeInformation
Append | Out-GridView instead of exporting if you want a sortable window.
11. Free space as a percentage, per volume
Get-Volume | Where-Object DriveLetter |
Select-Object DriveLetter, FileSystemLabel,
@{n='SizeGB';e={[math]::Round($_.Size/1GB,1)}},
@{n='FreePct';e={[math]::Round($_.SizeRemaining/$_.Size*100,1)}}
A percentage is easier to alert on than raw gigabytes. Anything under 10 to 15 percent on a system drive deserves attention.
12. Files changed in the last 7 days
Get-ChildItem C:\Reports -Recurse -File |
Where-Object LastWriteTime -gt (Get-Date).AddDays(-7) |
Sort-Object LastWriteTime -Descending |
Select-Object -First 10 Name, LastWriteTime
Replace C:\Reports with the folder you care about. Handy for “what changed before it broke?” questions.
13. Which process is listening on a port?
Get-NetTCPConnection -State Listen |
Sort-Object LocalPort |
Select-Object LocalAddress, LocalPort,
@{n='Process';e={(Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).ProcessName}}
Run it as Administrator to see the process name for every listener. Without elevation some rows may come back with an empty Process column.
14. Installed programs
Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' |
Where-Object DisplayName |
Sort-Object DisplayName |
Select-Object DisplayName, DisplayVersion
This reads the uninstall list from the registry. It is much faster than Get-WmiObject Win32_Product, which can also trigger Windows Installer repairs. 32-bit programs on a 64-bit Windows are listed under HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*.
15. Count and total things
# How many services are running and stopped?
Get-Service | Group-Object Status | Select-Object Name, Count
# Total memory used by all processes, in GB
[math]::Round((Get-Process | Measure-Object WorkingSet -Sum).Sum / 1GB, 2)
Group-Object counts rows per value. Measure-Object adds up, averages or finds the minimum and maximum of a property.
16. Run the same command on several servers
Invoke-Command -ComputerName server01, server02 -ScriptBlock { Get-Service W32Time }
This needs PowerShell remoting enabled on the target machines (Enable-PSRemoting run once on each) and an account that is an administrator there. Results come back with a PSComputerName column so you can tell the servers apart.
Common mistakes
- Format-Table in the middle of a pipeline.
Get-Service | Format-Table Name | Sort-Object Namedoes not work.Format-Tableturns your objects into display instructions, and the next command cannot sort or filter them. In Windows PowerShell 5.1 it ends in an error about Format objects, so format only at the very end. - Filtering late. When a command has its own filter parameter, use it:
Get-ChildItem -Filter *.sqlis much faster thanGet-ChildItem | Where-Object Extension -eq '.sql', because the filtering happens before PowerShell builds an object for every file. In a quick test on a large documents folder the-Filterversion took about 2.4 seconds and theWhere-Objectversion about 7.7. - Forgetting elevation. Event logs, some services and the process owner names only show fully when PowerShell runs as Administrator. If a result looks incomplete, try again elevated.
- Using text tools on objects. To save readable text to a file use
Out-File. To save data that can be reopened or imported, useExport-Csvas in example 10.
Turn a one-liner into a reusable command
When you use a one-liner often, wrap it in a function and save it in your PowerShell profile so it is there every time you open a window. Parameters make it flexible:
function Get-TopMemory {
param([int]$Count = 10)
Get-Process | Sort-Object WorkingSet -Descending |
Select-Object -First $Count Name, Id, @{n='MemoryMB';e={[int]($_.WorkingSet/1MB)}}
}
Get-TopMemory -Count 3
Your profile is the file whose path is stored in $PROFILE. Open it with notepad $PROFILE (create it first with New-Item -Force $PROFILE if it does not exist) and paste the function in. Next time you start PowerShell, Get-TopMemory is already available.
Tips for learning faster
- Use
Get-Help <command> -Examplesto see real usage for any cmdlet. - Pipe to
Get-Memberto discover the properties of whatever you are working with. - Add
-WhatIfto commands that change things (such asStop-ServiceorRemove-Item) to preview the effect first.
More tutorials like this
Why Did My Windows PC Restart or Crash? Find the Answer in Event Viewer and PowerShell
Find out why Windows restarted: the key event IDs (41, 6008, 1074, 1001), PowerShell commands to read them, and what to fix for each…
Bash Scripting for Beginners: Variables, Loops, Functions and Exit Codes
Learn Bash scripting from your first script to a safe backup script: variables, quoting, loops, functions, exit codes and set -euo pipefail, with real…
The Complete PowerShell Guide: Commands Explained with Examples
Learn PowerShell from the first command to real automation: cmdlets, the object pipeline, files, services, event logs, networking, Active Directory, remoting, scripts, error handling…



