PowerShell Language Reference

Welcome to the comprehensive language reference for PowerShell. This section provides detailed information on the syntax, structure, and elements that make up the PowerShell scripting language.

Core Concepts

Understanding the fundamental building blocks of PowerShell is crucial for effective scripting. This includes:

Variables

Variables in PowerShell are used to store values. They are declared using the dollar sign ($) followed by the variable name. PowerShell is dynamically typed, but you can explicitly define a type using type accelerators.

Syntax: $variableName = value

# Assigning a string
$myString = "Hello, PowerShell!"

# Assigning an integer
$myNumber = 42

# Assigning an array
$myArray = 1, 2, 3, 4, 5

# Explicitly typed variable
[int]$anotherNumber = 100
Tip: Variable names are case-insensitive by default, but it's good practice to maintain a consistent casing.

Data Types

PowerShell supports a wide range of .NET data types. Some common ones include:

  • [string]: Textual data.
  • [int]: 32-bit integers.
  • [long]: 64-bit integers.
  • [double]: Floating-point numbers.
  • [bool]: Boolean values ($true or $false).
  • [datetime]: Date and time values.
  • [array]: Collections of items.
  • [hashtable]: Key-value pairs.

You can discover the type of an object using the GetType() method:

$myVariable = Get-Process
$myVariable.GetType()

Operators

PowerShell uses operators for various operations:

Comparison Operators

  • -eq (Equal to)
  • -ne (Not equal to)
  • -gt (Greater than)
  • -ge (Greater than or equal to)
  • -lt (Less than)
  • -le (Less than or equal to)
  • -like (Pattern matching)
  • -match (Regular expression matching)

Arithmetic Operators

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulo)

Logical Operators

  • -and
  • -or
  • -xor
  • -not or !
if ($a -gt $b -and $c -eq 10) {
    Write-Host "Conditions met"
}

Control Flow Statements

Control flow statements dictate the order in which commands are executed.

If, ElseIf, Else

$score = 85
if ($score -ge 90) {
    Write-Host "Grade: A"
} elseif ($score -ge 80) {
    Write-Host "Grade: B"
} else {
    Write-Host "Grade: C or lower"
}

Switch Statement

$day = "Monday"
switch ($day) {
    "Monday" { Write-Host "Start of the week" }
    "Friday" { Write-Host "End of the week" }
    default  { Write-Host "Midweek" }
}

Loops (For, Foreach, While, Do-While)

Foreach Loop:

$names = "Alice", "Bob", "Charlie"
foreach ($name in $names) {
    Write-Host "Hello, $name!"
}

For Loop:

for ($i = 0; $i -lt 5; $i++) {
    Write-Host "Iteration: $i"
}

Functions

Functions allow you to encapsulate a series of commands into reusable blocks.

function Get-MyGreeting {
    param(
        [string]$Name = "World"
    )
    Write-Host "Greetings, $Name!"
}

Get-MyGreeting -Name "User"
Get-MyGreeting

Objects and Pipelines

PowerShell is object-oriented. Commands output objects, which are passed between commands through pipelines (using the | operator). This allows for powerful data manipulation.

# Get all running processes, sort them by CPU usage, and select the top 5
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
Tip: Use Get-Member to inspect the properties and methods of an object.
Get-Process | Get-Member

Further Reading