Make the choice

Save and run the code as a file? Choose Ps1File. Enter the code directly into a terminal? Choose TerminalPaste.

Compare the contexts

Ps1File and TerminalPaste
FeaturePs1FileTerminalPaste
SourceOne saved .ps1 fileInput handled by a terminal host and line editor
Top-level param()Binds values when the script is invokedA direct paste has no script-file invocation
#RequiresDeclares prerequisites for the scriptUse explicit checks or keep the code as a script
Script-path variables$PSScriptRoot and $PSCommandPath describe the running fileNo executing .ps1 path exists
Multiline inputThe parser receives the complete fileThe host decides when the interactive input is complete

Six checks are specific to TerminalPaste

PSRafScan 0.2.0-alpha1 applies 52 rules in both modes and adds these six for TerminalPaste:

  • FunctionInsidePasteWrapper
  • PasteSensitiveContinuationBlock
  • PasteSensitiveDoWhile
  • RequiresInTerminalPaste
  • ScriptFileVariableInTerminalPaste
  • TopLevelParamInTerminalPaste

Terminal behavior can vary

Interactive behavior can change with the terminal, PowerShell host, line editor, version, edit mode, and paste method.

RSX -Path ./saved-script.ps1 -IntendedUse Ps1File -FailOn Warning
RSX -Path ./paste-candidate.ps1 -IntendedUse TerminalPaste -FailOn Warning