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.
Scripts
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.
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.
script file → execution policy check → parsed → runs in its scope → exit code
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
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.
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
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 · 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.
- 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.
$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 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.
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 → Scripts. 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 → Scripts reference map
Fast lookup boundaries for PowerShell → Scripts.
| Item | Meaning | Evidence boundary |
|---|---|---|
| .ps1 file | Script source | file presence does not prove execution context |
| Parameter block | Declared inputs | validation does not prove external prerequisites |
| Script scope | Normal isolation boundary | dot-sourcing changes caller scope |
| Exit/error | Execution outcome signal | must account for non-terminating/native failures |
| Verification | Target-state check | stronger than script completion alone |