Running a script in a headless environment · PowerShell/PowerShell · Discussion #20291 · GitHub
Skip to content
Discussion options

You must be logged in to vote

Start PowerShell with -NonInteractive:

pwsh -NoProfile -NonInteractive -File ./script.ps1

This makes PowerShell host prompts fail instead of waiting for input. I tested your nonempty-directory example on PowerShell 7.6.5: Remove-Item reported PowerShell is in NonInteractive mode. Read and Prompt functionality is not available., and the directory remained intact.

See about_Pwsh -NonInteractive. It applies to PowerShell prompts, including Read-Host and confirmation prompts; external programs can have their own input behavior.

If the caller also requires a nonzero exit status, wrap the automation commands inside the script:

$ErrorActionPreference = 'Stop'
try {
    # Your automation commands…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@skycommand
Comment options

Answer selected by skycommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants