Skip to content
πŸ’» 🧠 Code 1001 > πŸ“‘ Cheat Sheets > Essential PowerShell Commands

Essential PowerShell Commands

1. File and Directory Management

Get-ChildItem (gci / ls / dir) – Lists files and directories.
Syntax: Get-ChildItem [-Path] <string> [-Recurse] [-Force]
Examples:

  • Get-ChildItem β†’ List items in current directory
  • Get-ChildItem -Path C:Users -Recurse β†’ Recursively list items
  • Get-ChildItem -Force β†’ Include hidden files

Set-Location (cd / sl) – Change current directory.
Syntax: Set-Location [-Path] <string>
Examples:

  • Set-Location C:Users β†’ Move to folder
  • Set-Location .. β†’ Move to parent directory

Copy-Item (cp / copy) – Copies files or folders.
Syntax: Copy-Item [-Path] <string> [-Destination] <string> [-Recurse] [-Force]
Examples:

  • Copy-Item .file.txt C:Backupfile.txt
  • Copy-Item .* C:Backup -Recurse β†’ Copy all files recursively

Move-Item (mv / move) – Moves files or directories.
Syntax: Move-Item [-Path] <string> [-Destination] <string> [-Force]
Examples:

  • Move-Item .file.txt C:Archivefile.txt

Remove-Item (rm / del / rmdir) – Deletes files or directories.
Syntax: Remove-Item [-Path] <string> [-Recurse] [-Force]
Examples:

  • Remove-Item .temp.txt β†’ Delete a file
  • Remove-Item C:Temp -Recurse -Force β†’ Delete folder with all contents

New-Item – Creates a new file or directory.
Syntax: New-Item [-Path] <string> [-ItemType <string>]
Examples:

  • New-Item -Path . -Name "NewFolder" -ItemType Directory
  • New-Item -Path . -Name "file.txt" -ItemType File

2. System and Process Management

Get-Process (ps / gps) – Lists running processes.
Syntax: Get-Process [-Name] <string>
Examples:

  • Get-Process β†’ Show all processes
  • Get-Process -Name notepad

Stop-Process (kill) – Stops a process.
Syntax: Stop-Process [-Name] <string> [-Id] <int> [-Force]
Examples:

  • Stop-Process -Name notepad
  • Stop-Process -Id 1234 -Force

Get-Service – Lists Windows services.
Syntax: Get-Service [-Name] <string>
Examples:

  • Get-Service β†’ List all services
  • Get-Service -Name wuauserv

Start-Service / Stop-Service – Starts or stops a service.
Syntax: Start-Service [-Name] <string> / Stop-Service [-Name] <string>
Examples:

  • Start-Service wuauserv
  • Stop-Service wuauserv

Restart-Computer – Restarts the system.
Syntax: Restart-Computer [-Force] [-Wait] [-Delay <int>]
Example:

  • Restart-Computer -Force

Get-EventLog – Retrieves event logs.
Syntax: Get-EventLog -LogName <string> [-Newest <int>]
Example:

  • Get-EventLog -LogName System -Newest 10 β†’ Last 10 system events

3. Network Management

Test-Connection (ping) – Tests connectivity.
Syntax: Test-Connection [-ComputerName] <string> [-Count <int>]
Example:

  • Test-Connection google.com -Count 4

Get-NetIPAddress – Shows IP configuration.
Syntax: Get-NetIPAddress
Example:

  • Get-NetIPAddress β†’ List all IP addresses

Resolve-DnsName – DNS query.
Syntax: Resolve-DnsName [-Name] <string>
Example:

  • Resolve-DnsName google.com

Get-NetTCPConnection – Displays TCP connections (like netstat).
Syntax: Get-NetTCPConnection
Example:

  • Get-NetTCPConnection

4. Environment and Variables

Get-Variable / Set-Variable / Remove-Variable – Manage PowerShell variables.
Syntax:

  • Get-Variable -Name <string>
  • Set-Variable -Name <string> -Value <object>
  • Remove-Variable -Name <string>
    Example:
  • Set-Variable -Name myVar -Value 42
  • Get-Variable -Name myVar
  • Remove-Variable -Name myVar

Get-ChildItem Env: – List environment variables.
Syntax: Get-ChildItem Env:
Example:

  • Get-ChildItem Env:PATH

5. Output and Formatting

Write-Output / Write-Host – Print text to console.
Syntax:

  • Write-Output "text"
  • Write-Host "text"
    Example:
  • Write-Output "Hello World"
  • Write-Host "Warning!" -ForegroundColor Red

Format-Table / Format-List – Format output.
Example:

  • Get-Process | Format-Table -AutoSize
  • Get-Service | Format-List Name, Status

Out-File / Tee-Object – Redirect output to file.
Example:

  • Get-Process | Out-File C:tempprocesses.txt
  • Get-Process | Tee-Object -FilePath C:tempprocesses.txt

6. Scripting & Control Flow

If / Else – Conditional execution.
Example:

if (Test-Path "C:tempfile.txt") {
    Write-Host "File exists"
} else {
    Write-Host "File not found"
}

ForEach-Object / ForEach – Loop through collection.
Example:

Get-ChildItem | ForEach-Object { Write-Host $_.Name }

Switch – Multi-condition branching.
Example:

switch ($day) {
    "Mon" { Write-Host "Monday" }
    "Tue" { Write-Host "Tuesday" }
}

Try / Catch / Finally – Error handling.
Example:

try {
    Remove-Item "C:tempnofile.txt"
} catch {
    Write-Host "File not found"
} finally {
    Write-Host "Operation finished"
}

Leave a Reply

Your email address will not be published. Required fields are marked *