Basics
Section titled “Basics”Get-Help <command> # Show help for a cmdletGet-Command <pattern> # Find commandsGet-Alias # List aliasesFiles & Directories
Section titled “Files & Directories”Get-ChildItem -Recurse # List all files recursivelyGet-ChildItem -File -Filter *.log # List .log filesSelect-String -Path *.txt -Pattern "error" # Search for text within filesCopy-Item -Path C:\\file.txt -Destination D:\\Backup\\ # Copy file to backupRemove-Item -Path *.tmp -Recurse -Force # Force delete all .tmp filesSystem & Processes
Section titled “System & Processes”Get-Process | Sort WS -Descending # List processes by memory usageStop-Process -Id <PID> -Force # Force kill a process by IDRestart-Computer -Force # Force reboot computerGet-Service | Where Status -eq "Running" # List only running servicesScripting & Automation
Section titled “Scripting & Automation”for ($i=1; $i -le 5; $i++) { Write-Host $i }foreach ($user in Get-LocalUser) { $user.Name }if (Test-Path "C:\\file.txt") { Remove-Item "C:\\file.txt" }$items = Get-ChildItem | Where-Object {$_.Length -gt 1MB} # Files > 1MBFiltering, Sorting, and Exporting
Section titled “Filtering, Sorting, and Exporting”Get-EventLog -LogName System | Where-Object {$_.EntryType -eq "Error"} | Sort-Object TimeGenerated -Descending | Select-Object -First 10
Get-Process | Export-Csv -Path C:\\proc.csv -NoTypeInformationImport-Csv -Path C:\\proc.csv | Where-Object { $_.CPU -gt 100 }Security
Section titled “Security”Get-LocalUser # List local usersSet-LocalUser -Name username -PasswordNeverExpires $trueGet-LocalGroupMember "Administrators" # List adminsAdd-LocalGroupMember -Group "Administrators" -Member "user1"Get-ExecutionPolicy # Current script execution policySet-ExecutionPolicy RemoteSigned # Set policyNetworking
Section titled “Networking”Test-Connection google.com -Count 4 # Ping testGet-NetIPAddress # Show IP infoGet-NetTCPConnection # List open TCP connectionsResolve-DnsName github.com # DNS lookupSystem Administration
Section titled “System Administration”Get-WmiObject Win32_BIOS # BIOS info (legacy)Get-CimInstance Win32_OperatingSystem # OS info (modern)Get-EventLog -LogName Security -Newest 20 # Last 20 security eventsRestart-Service -Name "Spooler" # Restart a serviceStart-Job -ScriptBlock { Get-Process } # Run async jobGet-Job; Receive-Job <Id> # List and retrieve jobsRegistry Editing
Section titled “Registry Editing”Get-ItemProperty -Path 'HKLM:\\Software\\...' # Read registrySet-ItemProperty -Path 'HKLM:\\Software\\...' -Name "<>" -Value "..."New-Item -Path 'HKCU:\\Software\\NewKey' # Create keyRemove-Item -Path 'HKCU:\\Software\\OldKey' -Recurse # Delete keyRemoting & Sessions
Section titled “Remoting & Sessions”Enter-PSSession -ComputerName server1 # Interactively connect to remote serverInvoke-Command -ComputerName server2 -ScriptBlock { Get-Process }New-PSSession -ComputerName server3 # Create a sessionUseful References
Section titled “Useful References”| Alias | Cmdlet |
|---|---|
| ls | Get-ChildItem |
| cd | Set-Location |
| pwd | Get-Location |
| cp | Copy-Item |
| mv | Move-Item |
| rm | Remove-Item |
| cat | Get-Content |
| ps | Get-Process |
Tip:
To see all commands available:
Get-Command