skip to content

Search

Syspirit
EN

PowerShell

Windows administration and automation with PowerShell!

PowerShell is an object-oriented shell and scripting language for system administration and automation.

📁 Navigation and Files

📌 Action🧠 Command
📍 Current directoryGet-Location or pwd
📂 Change directorySet-Location C:\Path or cd C:\Path
📋 List filesGet-ChildItem or ls
🔍 Recursive searchGet-ChildItem -Recurse
📄 Create a fileNew-Item -ItemType File file.txt
📁 Create a folderNew-Item -ItemType Directory folder
📋 Copy a fileCopy-Item source.txt destination.txt
➡️ Move a fileMove-Item file.txt C:\Temp\
🗑️ Delete a fileRemove-Item file.txt

🔧 Services and Processes

📌 Action🧠 Command
📋 List servicesGet-Service
▶️ Start a serviceStart-Service -Name "ServiceName"
⏹️ Stop a serviceStop-Service -Name "ServiceName"
🔄 Restart a serviceRestart-Service -Name "ServiceName"
📊 Service statusGet-Service -Name "ServiceName"
🔍 List processesGet-Process
❌ Kill a processStop-Process -Name "notepad"
💻 Processes by CPUGet-Process | Sort-Object CPU -Descending

💾 Variables and Types

📌 Action🧠 Syntax
📝 Simple variable$name = "John"
🔢 Typed variable[int]$age = 30
📚 Array$list = @("item1", "item2", "item3")
🗂️ Hash table$hash = @{Name="John"; Age=30}
🔤 String[string]$text = "Hello"
✅ Boolean[bool]$true_val = $true
📅 Date$date = Get-Date
🌐 Environment variable$env:COMPUTERNAME

🔍 Filtering and Pipeline

📌 Action🧠 Command
🔎 Filter with WhereGet-Service | Where-Object {$_.Status -eq "Running"}
📊 Select columnsGet-Process | Select-Object Name, CPU
📈 Sort resultsGet-Process | Sort-Object CPU -Descending
📋 First elementGet-Process | Select-Object -First 5
🎯 Search by nameGet-Service | Where-Object {$_.Name -like "*win*"}
📏 Measure objectsGet-Process | Measure-Object
🔄 ForEach in pipelineGet-Process | ForEach-Object {$_.Name}

💬 Display and Output

📌 Action🧠 Command
💬 Display textWrite-Host "Hello"
📄 Standard outputWrite-Output "Result"
⚠️ WarningWrite-Warning "Attention"
❌ ErrorWrite-Error "Error"
📝 To a fileGet-Process | Out-File processes.txt
📋 To clipboardGet-Process | Set-Clipboard
🎨 ColorsWrite-Host "Text" -ForegroundColor Red

🛠️ System Management

📌 Action🧠 Command
💻 System infoGet-ComputerInfo
🧠 Available memoryGet-CimInstance Win32_OperatingSystem
💿 Disk spaceGet-CimInstance Win32_LogicalDisk
👥 Local usersGet-LocalUser
🔐 Local groupsGet-LocalGroup
🌐 Network configurationGet-NetAdapter
📡 IP addressesGet-NetIPAddress
🔄 Restart computerRestart-Computer -Force

🔄 Control Structures

Conditions

# Basic If/Else
if ($age -ge 18) {
    Write-Host "Adult"
} else {
    Write-Host "Minor"
}
 
# Simple Switch
switch ($day) {
    "Monday"   { "Start of week" }
    "Friday"   { "Weekend is near" }
    default    { "Regular day" }
}

Loops

# Simple ForEach
$services = Get-Service
foreach ($service in $services) {
    Write-Host $service.Name
}
 
# Classic For
for ($i = 1; $i -le 5; $i++) {
    Write-Host "Counter: $i"
}

📜 Functions and Scripts

Basic Function

function Get-SystemInfo {
    param(
        [string]$ComputerName = $env:COMPUTERNAME
    )
 
    Write-Host "Information for: $ComputerName"
    Get-ComputerInfo | Select-Object TotalPhysicalMemory, CsProcessors
}
 
# Usage
Get-SystemInfo -ComputerName "MyPC"

Script with Parameters

param(
    [Parameter(Mandatory=$true)]
    [string]$ServiceName,
 
    [ValidateSet("Start", "Stop", "Restart")]
    [string]$Action = "Start"
)
 
switch ($Action) {
    "Start"   { Start-Service -Name $ServiceName }
    "Stop"    { Stop-Service -Name $ServiceName }
    "Restart" { Restart-Service -Name $ServiceName }
}
 
Write-Host "Action $Action performed on $ServiceName"

🛠️ Troubleshooting and Help

🆘 Problem🧠 Solution
❓ Help on a commandGet-Help Get-Process -Examples
🔍 Search for a commandGet-Command *service*
📋 View propertiesGet-Service | Get-Member
🚫 Execution errorSet-ExecutionPolicy RemoteSigned
📜 Command historyGet-History
🔧 PowerShell version$PSVersionTable