Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Scripts

Direct answer

A PowerShell script is a .ps1 file executed by a PowerShell host. Its behavior depends on engine/version, invocation method, parameters, current environment, modules, execution policy/signature rules, working directory and external dependencies.

When to use this

Use this topic when operationalizing repeatable automation, passing parameters, deciding between normal invocation and dot-sourcing, handling exit codes, making scripts portable, or diagnosing “works interactively but not scheduled/remotely” differences.

First 30 seconds

1. Get-ExecutionPolicy -List — allowed to run?

2. Blocked download? Unblock-File <script>.

3. Run with full path: & 'C:\Scripts\x.ps1'.

4. Add Set-StrictMode and $ErrorActionPreference='Stop'.

5. Log output; check exit code $LASTEXITCODE.

Operational verification

script file → execution policy check → parsed → runs in its scope → exit code

Evidence · next step

Exercise failure/rollback paths and add automated tests/preflight for unverified assumptions.

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

Do not use interactive prompts in unattended scripts unless explicitly supported. Do not assume current directory equals script directory. Do not set $ErrorActionPreference = Stop without understanding which commands produce non-terminating errors, and do not forget native exit codes.

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 operationalizing repeatable automation, passing parameters, deciding between normal invocation and …
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

Test scripts from the same execution context used in production: scheduled task, remote session, service account, CI runner, etc. Capture $PSVersionTable, working directory, user/token, module versions and parameter values (excluding secrets). Validate before changing and query target state after each critical stage.

and state what the evidence does not show.

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
  • Normal script invocation runs in a script scope; dot-sourcing deliberately executes in the caller scope.
  • Execution policy can affect script loading on Windows but is not a security boundary.
  • Script success depends on engine/module/dependency context, not file content alone.
  • PowerShell terminating errors, non-terminating errors and native process exit codes require explicit handling.
  • Reproducible scripts should make inputs, prerequisites, target scope and verification explicit.
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 scripts turn interactive commands into repeatable automation. Good scripts declare parameters, validate prerequisites, avoid hidden profile/global dependencies, log meaningful context and verify the outcome. $PSScriptRoot is useful for locating resources relative to the script rather than assuming the current working directory.

ReferenceImportant terms & boundaries3 sections · depth on demand

PowerShell boundary

Dot-sourcing (. .\script.ps1) loads script-defined functions/variables into the caller scope and should be an intentional design choice, not a workaround for scope bugs. Script signing and execution policy are loading/trust controls, while actual privilege/authorization remains enforced by Windows and target systems.

and state what the evidence does not show.

Platform, version and operating-envelope scope

Scripts run in Windows PowerShell 5.1 and PowerShell 7+; execution policy only applies on Windows, and PowerShell 7 uses its own profile paths and pwsh instead of powershell.exe. Declare requirements with #Requires -Version and #Requires -Modules.

In depth

Script structure that holds up in production:

#Requires -Version 7.2
#Requires -Modules ActiveDirectory
[CmdletBinding(SupportsShouldProcess)]
param([Parameter(Mandatory)][string]$OU, [int]$Days = 90)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$log = Join-Path $PSScriptRoot "logs\stale-$(Get-Date -f yyyyMMdd-HHmm).log"
Start-Transcript -Path $log
try {
    Get-ADComputer -SearchBase $OU -Filter * -Properties LastLogonDate |
        Where-Object LastLogonDate -lt (Get-Date).AddDays(-$Days) |
        ForEach-Object { if ($PSCmdlet.ShouldProcess($_.Name,'Disable')) { Disable-ADAccount $_ } }
} finally { Stop-Transcript }

Sign scripts if policy requires (Set-AuthenticodeSignature) and keep them in version control.

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 → Scripts. 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 scriptsps1 scripts windowspowershell psscriptrootpowershell script execution
Embedded reference data

Quick lookup tables

PowerShell → Scripts reference map

Fast lookup boundaries for PowerShell → Scripts.

ItemMeaningEvidence boundary
.ps1 fileScript sourcefile presence does not prove execution context
Parameter blockDeclared inputsvalidation does not prove external prerequisites
Script scopeNormal isolation boundarydot-sourcing changes caller scope
Exit/errorExecution outcome signalmust account for non-terminating/native failures
VerificationTarget-state checkstronger than script completion alone
Confirmation

Confirm action

Continue?