Access documentation

Open browser with help page about a command

Get-Help Get-Process -Online

Find commands related to a topic

Get-Command -Noun Process*

Get object properties

List available properties, methods for an object or command

h | Get-Member -MemberType Properties
  • abbreviate as h | gm
  • outputs Attributes, CreationTime, CreationTimeUtc, Exists, Extension, FullName, …

Output a property

$o = Get-Item .
$o.Attributes
  • one-liner: Get-Item . | select -expand Attributes

Configuration and Variables

Log to file

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
$PSDefaultParameterValues['Set-Content:Encoding'] = 'utf8'
"Hallo und Tschüss!" >> "test.log"
  • Set encoding, as PowerShell 5 uses different ones by default

Bundle configuration variables as the start of a script

$CONFIG = [PSCustomObject]@{
    SourcePath       = "E:\Data\Analytics\SourceData"
    LogFile          = "C:\scripts\DataExport.log"
    MaxLogLines      = 1000
}

Error Handling

Stop on any error

$ErrorActionPreference = [Management.Automation.ActionPreference]::Stop

Detect errors

try {
    Get-Content "file-that-might-not-exist.txt" -ErrorAction Stop
} catch {
    Write-Output "Error: $_"
} finally {
    Write-Output "Operation complete"
}

Comparison Operators

# String comparisons
"Text" -eq "text"   # Case-insensitive equal (True)
"Text" -ceq "text"  # Case-sensitive equal (False)
"Hello" -like "H*"  # Wildcard match (True)
"abc" -match "^a"   # RegEx match (True)
 
# Numeric comparisons
5 -gt 2     # Greater than (True)
5 -ge 5     # Greater than or equal (True)
5 -lt 10    # Less than (True)
 
# Logical operators
$true -and $false  # Logical AND (False)
$true -or $false   # Logical OR (True)
-not $true         # Logical NOT (False)