PowerShell Primer & Guide

A practical introduction to PowerShell covering commands, objects, scripting, automation, and administration fundamentals.

What is PowerShell?

PowerShell is Microsoft's command-line shell and scripting language designed for system administration, automation, configuration management, and task orchestration.

Unlike traditional shells that primarily pass text between commands, PowerShell passes objects. This allows commands to work with structured data rather than parsing text output.

PowerShell Editions

Launching PowerShell

Windows

Check Version

$PSVersionTable

PowerShell Command Structure

PowerShell commands are called cmdlets.

Verb-Noun

Examples:

Get-Process
Get-Service
Set-Location
Start-Service
Stop-Process
Verb Purpose
Get Retrieve data
Set Modify data
New Create resources
Remove Delete resources
Start Launch or begin
Stop Terminate or halt

Getting Help

Get-Help Get-Process
Get-Help Get-Service -Examples
Get-Help Get-ChildItem -Full

Discover Commands

Get-Command
Get-Command *process*
Get-Command -Verb Get

Variables

$name = "Alice"
$age = 30

Write-Output $name
Write-Output $age

Useful Variables

$HOME
$PWD
$PSVersionTable

Working with Objects

PowerShell returns objects with properties and methods.

Get-Process

Inspect available properties:

Get-Process | Get-Member

Select specific properties:

Get-Process |
    Select-Object Name, CPU, Id

The Pipeline

The pipeline (|) passes objects from one command to another.

Get-Service |
    Where-Object Status -eq "Running"
Get-Process |
    Sort-Object CPU -Descending |
    Select-Object -First 10

Files and Directories

Get-ChildItem
Get-ChildItem C:\Logs

New-Item file.txt
New-Item FolderName -ItemType Directory

Copy-Item file.txt backup.txt
Move-Item old.txt archive.txt
Remove-Item temp.txt

Aliases

ls
dir
cd
pwd

Filtering Data

Get-Process |
    Where-Object CPU -gt 100
Get-Service |
    Where-Object Name -like "*sql*"

Loops

ForEach

$numbers = 1..5

foreach ($n in $numbers) {
    Write-Output $n
}

For Loop

for ($i = 0; $i -lt 5; $i++) {
    Write-Output $i
}

Conditional Logic

$value = 10

if ($value -gt 5) {
    "Greater than 5"
}
else {
    "5 or less"
}

Comparison Operators

Operator Meaning
-eq Equal
-ne Not equal
-gt Greater than
-lt Less than
-like Pattern matching

Functions

function Get-Greeting {
    param(
        [string]$Name
    )

    "Hello, $Name"
}

Get-Greeting -Name "Alice"

Working with CSV Files

Import-Csv users.csv
Get-Process |
    Export-Csv processes.csv -NoTypeInformation

Error Handling

try {
    Get-Content missing.txt
}
catch {
    Write-Host "File not found"
}
finally {
    Write-Host "Finished"
}

Remote Administration

Enter-PSSession Server01
Invoke-Command -ComputerName Server01 `
    -ScriptBlock {
        Get-Service
    }

PowerShell Remoting enables remote management across systems.

Useful Everyday Commands

Get-Process
Get-Service
Get-EventLog
Get-Content
Test-Path
Restart-Service
Stop-Process
Get-NetIPAddress
Get-ComputerInfo

Script Example

# Check free disk space

$drives = Get-PSDrive -PSProvider FileSystem

foreach ($drive in $drives) {

    $freeGB = [math]::Round(
        $drive.Free / 1GB,
        2
    )

    Write-Output "$($drive.Name): $freeGB GB Free"
}

Best Practices

Learning Roadmap

  1. Learn cmdlets and help commands.
  2. Understand objects and the pipeline.
  3. Work with files and directories.
  4. Master filtering and sorting.
  5. Learn variables, loops, and conditions.
  6. Write reusable functions.
  7. Create scripts and automation workflows.
  8. Explore remoting and administration modules.

PowerShell Primer & Guide • Responsive HTML Reference