PowerShell: Common Commands Reference
Table of Contents
Section titled “Table of Contents”- PowerShell Usages
- Active Directory Operations
- File System Operations
- Process Management
- Service Management
- Network Operations
- String Manipulation
- Working with Arrays and Collections
- Pipeline and Filtering
- Error Handling
- Remoting
- Environment Variables
- Registry Operations
- Scheduled Tasks
- Useful Tips
- Resources
PowerShell Usages
Section titled “PowerShell Usages”Common PowerShell commands and patterns for system administration and automation on Windows.
Active Directory Operations
Section titled “Active Directory Operations”Lookup User Information
Section titled “Lookup User Information”Query Active Directory for user details:
Get-ADUser -Server localhost -Identity cs -Properties *Get specific properties:
Get-ADUser -Identity cs -Properties DisplayName, EmailAddress, DepartmentSearch for users by name:
Get-ADUser -Filter "Name -like '*John*'" -Properties DisplayName, EmailAddressGroup Management
Section titled “Group Management”List user’s group memberships:
Get-ADPrincipalGroupMembership -Identity cs | Select-Object NameAdd user to group:
Add-ADGroupMember -Identity "Developers" -Members csRemove user from group:
Remove-ADGroupMember -Identity "Developers" -Members cs -Confirm:$falseFile System Operations
Section titled “File System Operations”Navigation and Listing
Section titled “Navigation and Listing”# Change directorySet-Location C:\Users
# List filesGet-ChildItem
# List with detailsGet-ChildItem | Format-Table Name, Length, LastWriteTime
# Recursive searchGet-ChildItem -Recurse -Filter "*.log"File Operations
Section titled “File Operations”# Copy filesCopy-Item source.txt destination.txt
# Move filesMove-Item oldname.txt newname.txt
# Delete filesRemove-Item file.txt
# Create directoryNew-Item -ItemType Directory -Path C:\NewFolderReading and Writing Files
Section titled “Reading and Writing Files”# Read file contentGet-Content file.txt
# Write to file"Hello World" | Out-File file.txt
# Append to file"New line" | Add-Content file.txt
# Read as single stringGet-Content file.txt -RawProcess Management
Section titled “Process Management”Working with Processes
Section titled “Working with Processes”# List running processesGet-Process
# Filter by nameGet-Process -Name notepad
# Stop processStop-Process -Name notepad
# Start processStart-Process notepad.exe
# Get process by IDGet-Process -Id 1234Service Management
Section titled “Service Management”Service Operations
Section titled “Service Operations”# List all servicesGet-Service
# Get specific serviceGet-Service -Name wuauserv
# Start serviceStart-Service -Name wuauserv
# Stop serviceStop-Service -Name wuauserv
# Restart serviceRestart-Service -Name wuauserv
# Set service startup typeSet-Service -Name wuauserv -StartupType AutomaticNetwork Operations
Section titled “Network Operations”Network Information
Section titled “Network Information”# Get IP configurationGet-NetIPConfiguration
# Test connectionTest-Connection google.com
# Get network adaptersGet-NetAdapter
# DNS lookupResolve-DnsName google.comDownload Files
Section titled “Download Files”# Download fileInvoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
# Download and execute scriptInvoke-Expression (Invoke-WebRequest -Uri "https://example.com/script.ps1").ContentString Manipulation
Section titled “String Manipulation”Common String Operations
Section titled “Common String Operations”# String length$str = "Hello World"$str.Length
# Substring$str.Substring(0, 5) # "Hello"
# Replace$str.Replace("World", "PowerShell")
# Split$str.Split(" ")
# Join$words = @("Hello", "World")$words -join " "
# Case conversion$str.ToUpper()$str.ToLower()Working with Arrays and Collections
Section titled “Working with Arrays and Collections”Array Operations
Section titled “Array Operations”# Create array$arr = @(1, 2, 3, 4, 5)
# Access elements$arr[0]
# Add elements$arr += 6
# Filter array$arr | Where-Object { $_ -gt 3 }
# Transform array$arr | ForEach-Object { $_ * 2 }
# Sort array$arr | Sort-Object -DescendingHash Tables
Section titled “Hash Tables”# Create hash table$hash = @{ Name = "John" Age = 30 City = "New York"}
# Access values$hash["Name"]$hash.Age
# Add key-value pair$hash["Email"] = "john@example.com"
# Remove key$hash.Remove("City")Pipeline and Filtering
Section titled “Pipeline and Filtering”Using the Pipeline
Section titled “Using the Pipeline”# Basic pipelineGet-Process | Where-Object { $_.CPU -gt 100 } | Sort-Object CPU -Descending
# Select specific propertiesGet-Service | Select-Object Name, Status
# Group by propertyGet-Process | Group-Object ProcessName
# Measure objectsGet-ChildItem | Measure-Object -Property Length -SumError Handling
Section titled “Error Handling”Try-Catch-Finally
Section titled “Try-Catch-Finally”try { Get-Item "C:\nonexistent.txt" -ErrorAction Stop}catch { Write-Host "Error: $($_.Exception.Message)"}finally { Write-Host "Cleanup operations"}Remoting
Section titled “Remoting”Remote Execution
Section titled “Remote Execution”# Execute command on remote machineInvoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }
# Start interactive sessionEnter-PSSession -ComputerName Server01
# Execute script on remote machineInvoke-Command -ComputerName Server01 -FilePath C:\Scripts\script.ps1
# Copy to remote sessionCopy-Item -Path C:\local\file.txt -Destination C:\remote\ -ToSession $sessionEnvironment Variables
Section titled “Environment Variables”Working with Environment Variables
Section titled “Working with Environment Variables”# Get environment variable$env:PATH
# Set environment variable (current session)$env:MY_VAR = "value"
# Set permanently (user)[Environment]::SetEnvironmentVariable("MY_VAR", "value", "User")
# Set permanently (machine)[Environment]::SetEnvironmentVariable("MY_VAR", "value", "Machine")Registry Operations
Section titled “Registry Operations”Working with Registry
Section titled “Working with Registry”# Read registry valueGet-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion" -Name ProgramFilesDir
# Set registry valueSet-ItemProperty -Path "HKCU:\Software\MyApp" -Name "Setting" -Value "Value"
# Create registry keyNew-Item -Path "HKCU:\Software\MyApp"
# Delete registry keyRemove-Item -Path "HKCU:\Software\MyApp"Scheduled Tasks
Section titled “Scheduled Tasks”Task Management
Section titled “Task Management”# List scheduled tasksGet-ScheduledTask
# Create scheduled task$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\script.ps1"$trigger = New-ScheduledTaskTrigger -Daily -At 9amRegister-ScheduledTask -TaskName "MyTask" -Action $action -Trigger $trigger
# Run taskStart-ScheduledTask -TaskName "MyTask"
# Disable taskDisable-ScheduledTask -TaskName "MyTask"Useful Tips
Section titled “Useful Tips”Aliases
Section titled “Aliases”# List all aliasesGet-Alias
# Common aliases# ls -> Get-ChildItem# cd -> Set-Location# cat -> Get-Content# rm -> Remove-Item# cp -> Copy-Item# mv -> Move-ItemGetting Help
Section titled “Getting Help”# Get help for commandGet-Help Get-Process
# Get examplesGet-Help Get-Process -Examples
# Get detailed helpGet-Help Get-Process -Detailed
# Update help filesUpdate-HelpExecution Policy
Section titled “Execution Policy”# Check current execution policyGet-ExecutionPolicy
# Set execution policy (run as administrator)Set-ExecutionPolicy RemoteSigned
# Bypass for single scriptPowerShell.exe -ExecutionPolicy Bypass -File script.ps1