Use this when using PowerShell variables.
Variables · PowerShell Automation
PowerShell variables start with $, hold any object type and can be strongly typed with [int] or [string]. Scope modifiers like $script: and $global: control visibility.
1. Variables start with $.
2. Scopes: local, script, global.
3. StrictMode catches typos.
4. Typed variables ([int]$x).
5. Don't store secrets in plain variables written to transcripts.
value → $variable → scope
Context & applicabilityCommon mistakes · platform/version boundaries
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
Use Get-Variable, scope-qualified lookups and Get-ChildItem Env: to show where a value comes from. In functions or modules, test the value from the same scope in which automation executes. For parallel/remoting scenarios, explicitly validate what crosses via $using: or serialization and what remains local.
Step 2 Common mistakes Narrow the fault
- Variables leaking across scopes.
Step 3 Where it fails Narrow the fault
Variable existence is not evidence that the right scope or current value is being consumed. A global variable can create hidden coupling, environment variables can leak to child processes, and $using: captures references/values according to the execution boundary. Separate variable ownership, scope resolution, serialization/inheritance and target effects.
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.
- Variables start with $ and can be typed.
- Scopes include local, script and global.
- $using: passes values to remote or parallel blocks.
Get-Variable— variables in scopeSet-StrictMode -Version Latest— catch undefined variables$env:PATH— environment variables
ReferenceWhy it matters1 section · depth on demand
In depth
Variables in PowerShell hold objects of any type, and their scope decides where they are visible: inside a function, the script, or the whole session. Many automation bugs come from scope — a variable set inside a function is not visible outside it unless returned.
| Scope | Example | Visible in |
|---|---|---|
| Local (default) | $x = 1 | Current function/script block |
| Script | $script:count | Whole script file |
| Global | $global:cfg | Whole session (avoid) |
| Remote | $using:site | Inside Invoke-Command/-Parallel blocks |
| Environment | $env:API_TOKEN | Process environment |
Use Set-StrictMode -Version Latest to make references to undefined variables an error, and pass values into remote or parallel script blocks with $using: — local variables are not automatically available there.
ReferenceImportant terms & boundaries1 section · depth on demand
PowerShell mechanics
PowerShell variables live in scope containers associated with runspaces/session state. Local, script, global and private scope modifiers affect lookup and mutation, while automatic and preference variables are maintained by the engine. Environment variables are exposed through a provider and become process environment state; child processes inherit a snapshot rather than a live reference back to the parent session.
ReferenceConfiguration & state1 section · depth on demand
Authoring and lifecycle
Keep configuration inputs explicit as parameters or immutable data instead of relying on ambient global variables. Use scope modifiers only when the ownership boundary is intentional, and avoid mutating shared objects across parallel runspaces without thread-safe coordination. Secrets should be acquired through a secret provider or credential workflow rather than embedded in variables in source code.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Configuration & expected output
Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.
Configuration example
Assumptions: Review target identity, permissions and environment before mutation.
$script:Mode = "Audit"
function Get-Mode { $script:Mode }Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Microsoft Learn documentation reviewed for bounded PowerShell reference claims. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7.5 · confidence 94%Quick lookup tables
Variables · PowerShell operational reference
Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.
| Boundary | Variables reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Deep reference
Healthy vs suspicious evidence
Output interpretation
Variable display can hide type and scope differences; inspect .GetType() and the responsible scope when behavior differs. A value inherited into a child process or copied into a remote runspace can diverge from the original after creation. Preference variables can alter error, verbose and confirmation behavior without changing target state.