PowerShell Cheatsheet
PowerShell commands cheatsheet for server and network administration
94 commands
Get-ChildItemList files and folders
Get-ChildItem -Path C:\ -Recurse -Filter *.logCopy-ItemCopy files or folders
Copy-Item -Path .\src -Destination .\backup -RecurseMove-ItemMove or rename files/folders
Move-Item -Path .\old.txt -Destination .\new.txtRemove-ItemRemove files or folders
Remove-Item -Path .\temp -Recurse -ForceNew-ItemCreate a new file or folder
New-Item -Path .\logs -ItemType DirectoryGet-ContentRead file contents
Get-Content -Path .\app.log -Tail 50Set-ContentWrite content to a file (overwrite)
Set-Content -Path .\config.txt -Value 'setting=true'Add-ContentAppend content to a file
Add-Content -Path .\log.txt -Value 'New entry'Test-PathCheck if a path exists
Test-Path -Path C:\Windows\System32Get-ItemPropertyGet properties of a file or registry key
Get-ItemProperty -Path .\file.txt | Select-Object LastWriteTimeCompress-ArchiveCompress files into a ZIP archive
Compress-Archive -Path .\logs -DestinationPath .\logs.zipExpand-ArchiveExtract a ZIP archive
Expand-Archive -Path .\logs.zip -DestinationPath .\logsTest-NetConnectionTest network connectivity and ports
Test-NetConnection -ComputerName google.com -Port 443Get-NetIPAddressDisplay IP address configuration
Get-NetIPAddress -AddressFamily IPv4Get-NetAdapterList network adapters
Get-NetAdapter | Where-Object Status -eq 'Up'Resolve-DnsNamePerform DNS name resolution
Resolve-DnsName -Name example.com -Type MXGet-NetTCPConnectionList TCP connections
Get-NetTCPConnection -State EstablishedGet-NetRouteDisplay routing table
Get-NetRoute -AddressFamily IPv4New-NetIPAddressSet a new IP address
New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.1.100 -PrefixLength 24Set-DnsClientServerAddressSet DNS server address
Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 8.8.8.8,8.8.4.4Test-ConnectionSend ICMP ping requests
Test-Connection -ComputerName server01 -Count 4Invoke-WebRequestSend an HTTP request
Invoke-WebRequest -Uri https://api.example.com -Method GETInvoke-RestMethodSend a request to a REST API
Invoke-RestMethod -Uri https://api.example.com/data -Method POST -Body $jsonGet-ProcessList running processes
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10Stop-ProcessStop a process
Stop-Process -Name notepad -ForceStart-ProcessStart a new process
Start-Process -FilePath notepad.exe -ArgumentList .\file.txtGet-ServiceList services and their status
Get-Service | Where-Object Status -eq 'Running'Start-ServiceStart a service
Start-Service -Name W3SVCStop-ServiceStop a service
Stop-Service -Name W3SVC -ForceRestart-ServiceRestart a service
Restart-Service -Name SpoolerSet-ServiceChange service properties
Set-Service -Name W3SVC -StartupType AutomaticGet-JobList background jobs
Get-Job | Where-Object State -eq 'Running'Start-JobStart a background job
Start-Job -ScriptBlock { Get-EventLog -LogName System -Newest 100 }Get-ComputerInfoGet detailed computer information
Get-ComputerInfo | Select-Object OsName, OsVersion, CsTotalPhysicalMemoryGet-EventLogGet event log entries
Get-EventLog -LogName System -Newest 20 -EntryType ErrorGet-WinEventGet Windows event log entries
Get-WinEvent -LogName Application -MaxEvents 50Get-HotFixList installed hotfixes/updates
Get-HotFix | Sort-Object InstalledOn -DescendingGet-WmiObjectGet WMI object information
Get-WmiObject -Class Win32_OperatingSystem | Select-Object Caption, VersionGet-CimInstanceGet CIM instance (WMI successor)
Get-CimInstance -ClassName Win32_Processor | Select-Object Name, NumberOfCoresGet-CounterGet performance counter data
Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 2 -MaxSamples 5Get-DateGet and format current date/time
Get-Date -Format 'yyyy-MM-dd HH:mm:ss'Get-UptimeDisplay system uptime
Get-UptimeGet-WindowsFeatureList Windows features and roles
Get-WindowsFeature | Where-Object InstalledInstall-WindowsFeatureInstall a Windows feature
Install-WindowsFeature -Name Web-Server -IncludeManagementToolsGet-LocalUserList local users
Get-LocalUser | Where-Object EnabledNew-LocalUserCreate a local user
New-LocalUser -Name 'newuser' -Password (ConvertTo-SecureString 'P@ss' -AsPlainText -Force)Remove-LocalUserRemove a local user
Remove-LocalUser -Name 'olduser'Set-LocalUserModify local user properties
Set-LocalUser -Name 'user1' -PasswordNeverExpires $trueGet-LocalGroupList local groups
Get-LocalGroupAdd-LocalGroupMemberAdd a member to a local group
Add-LocalGroupMember -Group 'Administrators' -Member 'user1'Remove-LocalGroupMemberRemove a member from a local group
Remove-LocalGroupMember -Group 'Administrators' -Member 'user1'Get-LocalGroupMemberList members of a local group
Get-LocalGroupMember -Group 'Administrators'Get-VolumeDisplay volume information
Get-Volume | Where-Object DriveLetterGet-DiskList disks
Get-Disk | Select-Object Number, FriendlyName, Size, PartitionStyleGet-PartitionDisplay partition information
Get-Partition -DiskNumber 0Get-PSDriveList PowerShell drives
Get-PSDrive -PSProvider FileSystemNew-PartitionCreate a new partition
New-Partition -DiskNumber 1 -UseMaximumSize -AssignDriveLetterFormat-VolumeFormat a volume
Format-Volume -DriveLetter D -FileSystem NTFS -Confirm:$falseInitialize-DiskInitialize a disk
Initialize-Disk -Number 1 -PartitionStyle GPTGet-PhysicalDiskGet physical disk information
Get-PhysicalDisk | Select-Object FriendlyName, MediaType, Size, HealthStatusGet-NetFirewallRuleList firewall rules
Get-NetFirewallRule -Enabled True -Direction InboundNew-NetFirewallRuleCreate a new firewall rule
New-NetFirewallRule -DisplayName 'Allow HTTP' -Direction Inbound -Protocol TCP -LocalPort 80 -Action AllowRemove-NetFirewallRuleRemove a firewall rule
Remove-NetFirewallRule -DisplayName 'Allow HTTP'Set-NetFirewallRuleModify a firewall rule
Set-NetFirewallRule -DisplayName 'Allow HTTP' -Enabled FalseEnable-NetFirewallRuleEnable a firewall rule
Enable-NetFirewallRule -DisplayName 'Allow HTTP'Disable-NetFirewallRuleDisable a firewall rule
Disable-NetFirewallRule -DisplayName 'Allow HTTP'Get-NetFirewallProfileDisplay firewall profile settings
Get-NetFirewallProfile | Select-Object Name, EnabledSet-NetFirewallProfileConfigure firewall profile
Set-NetFirewallProfile -Profile Domain -Enabled TrueEnter-PSSessionStart an interactive remote session
Enter-PSSession -ComputerName server01 -Credential (Get-Credential)Exit-PSSessionEnd a remote session
Exit-PSSessionInvoke-CommandRun commands on remote computers
Invoke-Command -ComputerName server01,server02 -ScriptBlock { Get-Service W3SVC }New-PSSessionCreate a persistent remote session
New-PSSession -ComputerName server01 -Credential $credGet-PSSessionList remote sessions
Get-PSSessionRemove-PSSessionRemove a remote session
Get-PSSession | Remove-PSSessionEnable-PSRemotingEnable PowerShell remoting
Enable-PSRemoting -ForceCopy-Item -ToSessionCopy files to a remote session
Copy-Item -Path .\script.ps1 -Destination C:\Scripts -ToSession $sessionGet-IISSiteList IIS websites
Get-IISSiteStart-IISSiteStart an IIS website
Start-IISSite -Name 'Default Web Site'Stop-IISSiteStop an IIS website
Stop-IISSite -Name 'Default Web Site'New-IISSiteCreate a new IIS website
New-IISSite -Name 'MySite' -PhysicalPath C:\inetpub\mysite -BindingInformation '*:8080:'Get-IISAppPoolList IIS application pools
Get-IISAppPool | Select-Object Name, StateImport-Module WebAdministrationImport the IIS management module
Import-Module WebAdministrationGet-WebBindingDisplay website binding information
Get-WebBinding -Name 'Default Web Site'New-WebBindingAdd a binding to a website
New-WebBinding -Name 'MySite' -Protocol https -Port 443 -HostHeader mysite.example.comGet-ADUserSearch Active Directory users
Get-ADUser -Filter * -SearchBase 'OU=Users,DC=example,DC=com' -Properties EmailAddressNew-ADUserCreate an AD user
New-ADUser -Name 'John Smith' -SamAccountName jsmith -Enabled $true -AccountPassword (ConvertTo-SecureString 'P@ss' -AsPlainText -Force)Set-ADUserModify AD user properties
Set-ADUser -Identity jsmith -Department 'IT' -Title 'Engineer'Remove-ADUserRemove an AD user
Remove-ADUser -Identity jsmith -Confirm:$falseGet-ADComputerSearch AD computer objects
Get-ADComputer -Filter * -Properties OperatingSystem | Select-Object Name, OperatingSystemGet-ADGroupSearch AD groups
Get-ADGroup -Filter 'Name -like "IT*"'Add-ADGroupMemberAdd a member to an AD group
Add-ADGroupMember -Identity 'IT-Team' -Members jsmithGet-ADGroupMemberList members of an AD group
Get-ADGroupMember -Identity 'Domain Admins' -RecursiveGet-ADOrganizationalUnitList AD organizational units
Get-ADOrganizationalUnit -Filter * | Select-Object Name, DistinguishedNameUnlock-ADAccountUnlock a locked AD account
Unlock-ADAccount -Identity jsmith