Post

Powershell Commands - CMDSheet

Powershell Commands - CMDSheet

Powershell CMDSheet

  • PowerShell provides an extensive set of commands to handle various administrative and scripting tasks.
  • Below is a categorized list of PowerShell commands commonly used by system administrators and IT professionals.

File System Management

CommandDescription
Get-ChildItemLists files and directories in the specified path.
New-ItemCreates a new file or directory.
Remove-ItemDeletes files or directories.
Copy-ItemCopies files or directories.
Move-ItemMoves files or directories.

Example:

1
Get-ChildItem -Path C:\Scripts -Recurse

Service Management

CommandDescription
Get-ServiceLists all services on the system.
Start-ServiceStarts a specific service.
Stop-ServiceStops a specific service.
Restart-ServiceRestarts a specific service.

Example:

1
Restart-Service -Name "Spooler"

Process Management

CommandDescription
Get-ProcessLists running processes.
Stop-ProcessStops a running process.
Start-ProcessStarts a new process.

Example:

1
Get-Process | Where-Object { $_.CPU -gt 100 }

Environment Variables

CommandDescription
$Env:<Variable>Accesses environment variables.
[Environment]::SetEnvironmentVariableSets a new environment variable.

Example:

1
2
$Env:Path
[Environment]::SetEnvironmentVariable("MyVar", "Value", "User")

Networking

CommandDescription
Test-ConnectionPings a remote system.
Get-NetIPAddressRetrieves network adapter IP configurations.
New-NetIPAddressAssigns a static IP address.

Example:

1
Test-Connection -ComputerName "google.com" -Count 3

User and Group Management

CommandDescription
Get-LocalUserLists local user accounts.
New-LocalUserCreates a new local user.
Add-LocalGroupMemberAdds a user to a local group.

Example:

1
Get-LocalUser

File Parsing

  • JSON

    1
    2
    3
    
    # Parse JSON file
    $jsonContent = Get-Content -Path .\data.json | ConvertFrom-Json
    $jsonContent
    
  • YAML

    • Install the powershell-yaml module:
    1
    
    Install-Module -Name powershell-yaml
    
    • Parse YAML:
    1
    2
    
    $yamlContent = Get-Content -Path .\data.yaml | ConvertFrom-Yaml
    $yamlContent
    
  • CSV

    1
    2
    3
    
    # Parse CSV file
    $csvContent = Import-Csv -Path .\data.csv
    $csvContent
    
  • XML

    1
    2
    3
    
    # Parse XML file
    $xmlContent = [xml](Get-Content -Path .\data.xml)
    $xmlContent
    

Operators

  • For Loop

    1
    2
    3
    
    for ($i = 1; $i -le 5; $i++) {
      Write-Output "Number: $i"
    }
    
  • Do-While Loop

    1
    2
    3
    4
    5
    
    $count = 1
    do {
        Write-Output "Count: $count"
        $count++
    } while ($count -le 5)
    
  • ForEach

    1
    2
    3
    4
    
    $items = 1..5
    foreach ($item in $items) {
        Write-Output "Processing item: $item"
    }
    

Miscellaneous Commands

CommandDescription
Get-EventLogRetrieves system event logs.
Export-ClixmlExports data to an XML file.
Export-CsvExports data to a CSV file.

Example:

1
Get-EventLog -LogName System -Newest 10 | Export-Csv -Path .\eventlog.csv -NoTypeInformation
This post is licensed under CC BY 4.0 by the author.