Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Variables

Direct answer

PowerShell variables store references to values/objects and participate in scope rules. A variable can be local, script, global or otherwise scoped; automatic and preference variables expose or control engine behavior, while environment variables are surfaced through the Env: provider and affect process environment.

When to use this

Use this topic when values unexpectedly change/disappear, functions read the wrong scope, scripts depend on preference variables, environment changes fail to affect existing processes, or type conversion changes a value.

First 30 seconds

1. Get-Variable <name> — value and scope.

2. Type: $x.GetType().

3. Script vs global scope: $script: / $global:.

4. Set-StrictMode -Version Latest to catch typos.

5. Quote strings: double quotes expand $var.

Operational verification

variable assigned → typed object stored in a scope → read later in that scope

Evidence · next step

Inspect the authoritative configuration source or the next consuming scope/process.

Continue only after interpreting the current observation.
Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common traps

Avoid using global scope to fix a scope misunderstanding. Avoid overwriting automatic variables. Do not assume assigning an environment variable changes Windows persistent configuration or updates already-running processes.

Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this topic when values unexpectedly change/disappear, functions read the wrong scope, scripts depend on preference …
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

1 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Verification path Verify boundary

When a value differs unexpectedly, query it in each relevant scope and inspect its type. Reproduce in a clean session to expose profile/global contamination. For environment-driven failures, compare the current process value, persistent source, and a newly launched child process.

Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • Variable name and scope are part of identity; the same name can exist in multiple scopes.
  • Assignment can replace a variable reference without changing the source object elsewhere, while mutable objects can still be shared.
  • Automatic variables are engine-provided and should not be casually repurposed.
  • Preference variables alter PowerShell behavior but are not equivalent to cmdlet parameters in every context.
  • Environment variables in a process are runtime state; persistent configuration and child-process inheritance are separate boundaries.
Key checks
  • $PSVersionTable — PowerShell edition and version
  • Get-Command <name> — which cmdlet/function/exe runs
  • Get-Help <cmdlet> -Full — syntax and parameters
  • Get-ExecutionPolicy -List — execution policy per scope
ReferenceWhat it is1 section · depth on demand

Core model

PowerShell scope is hierarchical. Functions and scripts can create local variables, access parent scope values and explicitly target script/global scopes. Parameters are variables in the function/script scope. Understanding this model prevents hidden dependencies on caller state.

ReferenceImportant terms & boundaries2 sections · depth on demand

PowerShell boundary

PowerShell variables can hold any .NET object unless type-constrained. Type accelerators or explicit type declarations can force conversion on assignment. $env:NAME accesses the current process environment, which is distinct from user/machine persistent environment configuration and from already-running sibling processes.

In depth

Variables start with $, are not strongly typed unless you cast them, and have scopes (global, script, local, private, using).

$name = 'srv01'
[int]$count = '5'
$env:PATH -split ';'
$path = "C:\Logs\$name-$(Get-Date -Format yyyyMMdd).log"
Set-StrictMode -Version Latest     # errors on undefined variables

Automatic variables: $_/$PSItem (current pipeline object), $? (last success), $LASTEXITCODE (native exe exit code), $Error, $PSScriptRoot. Use $using:var to pass local variables into remote or parallel script blocks.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

Authoritative/upstream documentation reviewed for bounded claims in PowerShell → Variables. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://learn.microsoft.com/en-us/powershell/ · confidence 94%
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
powershell variablespowershell variable scopeautomatic variables powershellpowershell env variables
Embedded reference data

Quick lookup tables

PowerShell → Variables reference map

Fast lookup boundaries for PowerShell → Variables.

ItemMeaningEvidence boundary
Local/script/global scopeName-resolution boundarysame name may hold different values
Automatic variableEngine-maintained statemeaning/lifecycle is variable-specific
Preference variableDefault behavior controlcmdlet explicit parameter can override behavior
Env: variableProcess environmentnot proof of persisted machine/user environment
Typed variableConversion/constraintassignment can fail or coerce
Confirmation

Confirm action

Continue?