KevinZH
KevinZH

Reputation: 19

how to automatically answer Yes to multiple prompt in powershell

I am trying to automatically answer multiple 'Yes' on below command Install-Module -Name PSWindowsUpdate -Force -Confirm:$false

I checked below url and it only answer one y, but trying to add 2 'y' as below, it does not work. is there any method we can make it ?

cmd /c echo y y| powershell "the command which will propmt"

Automatically confirm Yes to powershell script

Upvotes: 1

Views: 5072

Answers (1)

mklement0
mklement0

Reputation: 437042

From PowerShell:

, 'y' * 2  | powershell -c 'read-host one; read-host two'

, 'y' * 2 constructs a 2-element array consisting of 'y' strings (the equivalent of 'y', 'y').

Used as pipeline input for an external program (which happens to be another PowerShell process in this case), PowerShell sends each element followed by a newline, which each answers one prompt in the PowerShell code.


From cmd.exe:

(echo y& echo y) | powershell -c "read-host one; read-host two"

Note: The use of (...) in a pipeline apparently invariably appends a space to each echo command's output, so that each prompt in PowerShell receives 'y <Enter>'; this shouldn't be a problem, but if it is, you can use an intermediate temporary file:
(echo y& echo y) >tmp.txt & type tmp.txt | powershell ... & del tmp.txt

Upvotes: 7

Related Questions