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.
Variables
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.
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.
variable assigned → typed object stored in a scope → read later in that scope
Inspect the authoritative configuration source or the next consuming scope/process.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
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.
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
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 · 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.
- 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.
$PSVersionTable— PowerShell edition and versionGet-Command <name>— which cmdlet/function/exe runsGet-Help <cmdlet> -Full— syntax and parametersGet-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.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
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%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.
Quick lookup tables
PowerShell → Variables reference map
Fast lookup boundaries for PowerShell → Variables.
| Item | Meaning | Evidence boundary |
|---|---|---|
| Local/script/global scope | Name-resolution boundary | same name may hold different values |
| Automatic variable | Engine-maintained state | meaning/lifecycle is variable-specific |
| Preference variable | Default behavior control | cmdlet explicit parameter can override behavior |
| Env: variable | Process environment | not proof of persisted machine/user environment |
| Typed variable | Conversion/constraint | assignment can fail or coerce |