Skip to content

Latest commit

 

History

History
277 lines (190 loc) · 5.33 KB

CheatSheet-Standard.md

File metadata and controls

277 lines (190 loc) · 5.33 KB

PowerShell Cheat Sheet - Standard

Basic Commands

  • Get-Help – Displays information about commands.

    Get-Help <Command> -Full
  • Get-Command – Lists available cmdlets, functions, workflows, aliases.

    Get-Command -Name <Pattern> -Module <ModuleName>
  • Get-Alias – Lists aliases for cmdlets.

    Get-Alias

Core Cmdlets

  • Get-Process – Displays running processes.

    Get-Process | Where-Object {$_.CPU -gt 100}
  • Get-Service – Lists system services.

    Get-Service | Where-Object {$_.Status -eq 'Stopped'}
  • Get-EventLog – Retrieves event logs.

    Get-EventLog -LogName Application -Newest 10
  • Test-Connection – Network connectivity check (similar to ping).

    Test-Connection -ComputerName 'hostname' -Count 2

Data Manipulation

  • Where-Object – Filters objects based on condition.

    Get-Process | Where-Object {$_.CPU -gt 100}
  • Select-Object – Selects specific properties.

    Get-Service | Select-Object -Property Name, Status
  • Sort-Object – Sorts objects by property.

    Get-Process | Sort-Object CPU -Descending
  • Group-Object – Groups objects by property.

    Get-EventLog -LogName System | Group-Object -Property Source
  • Measure-Object – Calculates statistical data.

    Get-Process | Measure-Object -Property CPU -Sum

File & Directory Operations

  • Get-ChildItem (ls, dir, gci) – Lists files/directories.

    Get-ChildItem -Path C:\ -Recurse -Filter "*.log"
  • New-Item – Creates files or directories.

    New-Item -Path 'C:\Logs' -ItemType Directory
  • Remove-Item – Deletes files or directories.

    Remove-Item -Path 'C:\Logs\old.log' -Force
  • Copy-Item – Copies files or directories.

    Copy-Item -Path 'C:\source.txt' -Destination 'C:\backup\'
  • Move-Item – Moves files or directories.

    Move-Item -Path 'C:\temp\file.txt' -Destination 'C:\archive\'

Text & String Manipulation

  • Select-String – Searches for text patterns.

    Get-Content 'C:\Logs\log.txt' | Select-String -Pattern 'Error'
  • -replace – Replaces text within strings.

    "Hello World" -replace "World", "PowerShell"
  • -match / -notmatch – Regex pattern matching.

    $text -match '\d{3}-\d{2}-\d{4}'

Variables & Objects

  • New-Variable – Creates variables.

    New-Variable -Name 'MyVar' -Value 'Hello'
  • Remove-Variable – Deletes variables.

    Remove-Variable -Name 'MyVar'
  • Select-Object -ExpandProperty – Accesses properties.

    $object | Select-Object -ExpandProperty Name

Scripting & Automation

  • ForEach-Object – Iterates through items.

    Get-Process | ForEach-Object { $_.Name }
  • If/Else Statements – Conditional execution.

    if ($var -eq 10) { "Equal" } else { "Not equal" }
  • Switch Statement – Simplifies conditional logic.

    switch ($value) {
      1 { "One" }
      2 { "Two" }
      Default { "Other" }
    }
  • Try/Catch/Finally – Error handling.

    try {
      Get-Content 'file.txt'
    } catch {
      Write-Output "Error!"
    } finally {
      Write-Output "Done"
    }
  • Function Creation – Defines reusable code blocks.

    function Get-CustomProcess {
      param($Name)
      Get-Process -Name $Name
    }

Advanced Functions

  • CmdletBinding – Advanced function with pipeline support.

    function Get-SampleProcess {
        [CmdletBinding()]
        param([Parameter(Mandatory=$true, ValueFromPipeline=$true)] $Name)
        process { Get-Process -Name $Name }
    }
  • Dynamic Parameters – Define parameters at runtime.

    function Test-DynamicParam {
        param()
        dynamicparam {
            # Code to create dynamic parameter
        }
    }

Modules & Importing

  • Import-Module – Loads a module into the session.

    Import-Module ActiveDirectory
  • Get-Module – Lists loaded modules.

    Get-Module

Remote Management

  • Enter-PSSession – Starts an interactive session on a remote computer.

    Enter-PSSession -ComputerName 'RemotePC'
  • Invoke-Command – Runs commands on a remote computer.

    Invoke-Command -ComputerName 'RemotePC' -ScriptBlock { Get-Service }
  • New-PSSession – Creates a persistent remote session.

    $session = New-PSSession -ComputerName 'RemotePC'
  • Import-PSSession – Imports commands from a remote session.

    Import-PSSession -Session $session

Error Handling

  • $ErrorActionPreference – Controls error action.

    $ErrorActionPreference = "Stop"
  • Try/Catch – Structured error handling.

    try {
        # Command that may fail
    } catch {
        Write-Output "Caught error"
    }