Code Reference

Variables

PowerShell · Reference cheat sheet

Variables

PowerShell · Reference cheat sheet


📋 Overview

Variables start with $ and hold typed .NET objects. Scopes include local, script, global, and private. Prefer meaningful names; use $script: for module/script-level state and avoid polluting $global:.

🔧 Core concepts

SyntaxMeaning
$name = valueAssign
$\{name-with-dashes\}Unusual names
$env:PATHEnvironment variable
$nullNull
Remove-Variable / rvDelete
Get-VariableInspect
Set-Variable -Option ReadOnlyProtect
$true / $falseBooleans
[int]$n = 5Constrained type
$$ / $^Last/first token of last line (interactive)

Automatic variables: $_, $PSItem, $args, $PSScriptRoot, $PSCommandPath, $LASTEXITCODE, $?, $Error.

💡 Examples

Types and env:

[int]$port = 8080
$name = 'Ada'
$env:NODE_ENV = 'development'
"$name runs on $port"

Scopes:

$script:counter = 0
function Tick {
  $script:counter++
  $local = 'only here'
}

Null-conditional / coalescing (PS 7+):

$value = $config?.Timeout ?? 30

Splatting:

$params = @{ Path = '.\a.txt'; Destination = '.\b.txt'; Force = $true }
Copy-Item @params

⚠️ Pitfalls

  • $false, 0, empty string, and empty arrays are all “falsy” in conditionals—know the rules.
  • Assigning to $env:VAR affects the process environment only (not permanently unless you set user/machine env).
  • $? is boolean success of last PowerShell statement; native commands also set $LASTEXITCODE.
  • Variable names are not case-sensitive.
  • Expanding in double quotes calls .ToString()—objects may stringify poorly; use subexpressions "$($obj.Prop)".
  • Don’t use $input casually—it’s reserved for pipeline enumerators in functions.

On this page