Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Variables · PowerShell Automation

Direct answer

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.

When to use this

Use this when using PowerShell variables.

First 30 seconds

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.

Operational verification

value → $variable → scope

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
PowerShell 7.5/current
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.

PlatformPowerShell 7.5/current · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when using PowerShell variables.
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.

3 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

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 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
  • Variables start with $ and can be typed.
  • Scopes include local, script and global.
  • $using: passes values to remote or parallel blocks.
Key checks
  • Get-Variable — variables in scope
  • Set-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.

ScopeExampleVisible in
Local (default)$x = 1Current function/script block
Script$script:countWhole script file
Global$global:cfgWhole session (avoid)
Remote$using:siteInside Invoke-Command/-Parallel blocks
Environment$env:API_TOKENProcess 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
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
Technical examples

Configuration & expected output

Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.

Configuration example

PowerShell 7.5/current 7.5/currentMinimal authored PowerShell pattern for Variables

Assumptions: Review target identity, permissions and environment before mutation.

$script:Mode = "Audit"
function Get-Mode { $script:Mode }
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 docPowerShell 7.5/current 7.5/current

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%
Embedded reference data

Quick lookup tables

Variables · PowerShell operational reference

Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.

BoundaryVariables reference
Runtime identityPowerShell edition/version + command/module source
Persistent intentScript/module/config prerequisites under source control
VerificationRead-only discovery → execution evidence → independent target read-back
Output contractStructured objects/streams before formatting
Failure boundarySeparate engine/binding from transport/API/native and target-state failures
Depth when you need it

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.

Confirmation

Confirm action

Continue?