Dig Durr
Dig Durr

Reputation: 21

Why is PowerShell script pausing when prompted for input?

I successfully ran 3 commands in my PowerShell script shortcut but after running the last command it prompts the user for "y" or "N" and in my script I want to input "y" and press enter.

After looking at other posts such as this I have tried to ECHO the keystroke but it only works prior to the last command Here you can see the 1st Echo Successfully and the 2nd is stuck because it is paused or something

PowerShell Script

ECHO 'y'

spicetify config current_theme Sleek
spicetify config color_scheme futura
spicetify apply

ECHO 'y'

Upvotes: 2

Views: 116

Answers (1)

mklement0
mklement0

Reputation: 438093

  • To feed automated responses to external programs that request interactive input, pipe those responses to them, which they receive via their stdin stream.

  • Note that, as of PowerShell 7.3.1, a trailing newline (CRLF on Windows, LF on Unix) is automatically and invariably appended to a string you output from PowerShell so that, on Windows, sending 'y' in effect sends "y`r`n", and that newline is usually interpreted as submitting input to an interactive prompt.

    • While this can be convenient, there are situations in which it is problematic - see this answer.

Therefore, for every one among your spicetify calls that may / does prompt for a y/n response, do the following, using your last command as the example:

'y' | spicetify apply

Note how string literal 'y' by itself is enough to produce output - no need for echo, which in PowerShell is an alias for the rarely needed Write-Output cmdlet - see this answer for more information.

Upvotes: 2

Related Questions