Skip to content

PowerShell Syntax

Learn the core rules for structuring PowerShell statements, cmdlets, variables, and code blocks to write functional scripts and commands.

PowerShell syntax defines the rules for how statements, commands, and code blocks must be structured for the engine to interpret them correctly. We will cover the foundational grammar rules you'll use in every script.

Basic Statement Structure

A PowerShell statement is a single instruction, typically a cmdlet call, an assignment, or a control-flow construct. Statements are generally written one per line, and PowerShell reads and executes them sequentially, top to bottom.

basic-syntax.ps1powershell
$name = "Tom"
Write-Output "Hello, $name!"

where:

  • $name = "Tom" is an assignment statement, storing a string value in the variable $name.
  • Write-Output "Hello, $name!" is a command statement, calling a cmdlet with a string argument.
Consolepowershell
./basic-syntax.ps1
Outputplaintext
Hello, Tom!

A typical cmdlet statement follows this general pattern:

Verb-Noun -ParameterName ParameterValue -SwitchParameter

Case-Insensitivity

PowerShell is case-insensitive for cmdlet names, parameter names, variable names, and most keywords. This differs from Bash, which is case-sensitive throughout.

Consolepowershell
GET-PROCESS -NAME "pwsh"
get-process -name "pwsh"
Get-Process -Name "pwsh"

All three commands above are functionally identical and produce the same output.

Line Continuation

By default, PowerShell expects one statement per line. To split a single logical statement across multiple lines, use the backtick ( ` ), PowerShell's line continuation character.

line-continuation.ps1powershell
Write-Output `
    "This message " `
    + "spans multiple lines."

where:

  • The trailing backtick ( ` ) at the end of each line tells PowerShell the statement continues on the next line.
  • There must be no characters (not even a trailing space) after the backtick, or the continuation will fail.
Outputplaintext
This message spans multiple lines.
pipe-continuation.ps1powershell
Get-Process |
    Where-Object { $_.CPU -gt 5 } |
    Select-Object Name, CPU

Semicolons for Multiple Statements Per Line

Use a semicolon (;) to place multiple statements on a single line.

Consolepowershell
$a = 5; $b = 10; Write-Output ($a + $b)
Outputplaintext
15

where each ; separates a distinct, independent statement, executed left to right.

Comment Syntax Preview

PowerShell supports single-line comments with # and block comments with <# ... #>.

Consolepowershell
Write-Output "Comments are ignored by the engine"

A full breakdown of comment styles, including comment-based help, is covered in PowerShell Comments.

Code Blocks with

A code block (also called a script block) is a group of one or more statements enclosed in curly braces { }. Code blocks are used with control-flow constructs (if, foreach, while), functions, and cmdlets like Where-Object and ForEach-Object.

code-block.ps1powershell
if ($true) {
    Write-Output "Inside the code block"
}

where { } delimits the block of statements that execute when the if condition evaluates to $true.

Outputplaintext
Inside the code block

Code blocks can also be stored in a variable as a script block object and invoked later using the call operator &:

Consolepowershell
$greet = { Write-Output "Hello from a stored script block!" }
& $greet
Outputplaintext
Hello from a stored script block!

:::note Braces Define Scope Boundaries

Every { } block in PowerShell, whether attached to an if, a function, or a loop, establishes its own local variable scope. See Local Variables in Functions for how this affects variable visibility.

:::```

Navigation