39

How can I input "yes" as an answer to an interactive question in a PowerShell session? I know, in Bash, Yes is the tool to answer "yes" on the prompt. In my situation, I can't suppress the prompt.

The script I'm running stops at

Please reply "yes" if you want to continue:

How powershell can run the script and "answer" yes when pompted?

Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
Frown
  • 417
  • 1
  • 5
  • 8
  • 3
    Need more context, for instance what is cfgsave ? Is it something you wrote or are you just trying to use Powershell to run it. How would cfgsave work without Powershell, it sounds like it would display a window with a 'yes' button? – Oliver Gray Dec 28 '12 at 14:39
  • This is asking for a way to pass input to a shell prompt. – Peter Kionga-Kamau Sep 24 '21 at 14:01

13 Answers13

22

Using ECHO worked for me. I am running RSKeyMgmt.exe in my Powershell script and it prompted Yes(Y)/ No(N). So when I do

ECHO Y | RSKeyMgmt.exe...

It did not prompt the question and the command was executed correctly.

So I think ECHO 'someoption' should work for other cases too.

Dongminator
  • 795
  • 10
  • 15
21

I was also having the same issue. The solutions tried by me included:

  • ECHO 'Y' |
  • Command-Name -Force
  • $ConfirmPreference = 'None'

However, none of them seemed to do the trick.

What finally solved the problem was:

Powershell-Cmdlet -Confirm:$false

It suppresses all confirmation prompts for the duration of the command and the command would be processed without any confirmation.

AJain2810
  • 375
  • 3
  • 11
12

Inspired by other answers, here is my example:

ECHO Y | powershell Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
Roy
  • 201
  • 3
  • 3
8

You can test these old fashion ways :

Cmd /c "GpUpdate.exe /FORCE <C:\temp\response.txt"

ECHO 'Y' | GpUpdate.exe /FORCE

Get-Content "C:\temp\response.txt" | GpUpdate.exe /FORCE
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
  • I am just using powershell to run it. CFGsave is an external command run on a switch. I would like to run this command from powershell without having to respond to yes or no. Also thanks JP but i would prefer powershell workaround if it exists. :) for my learning more than anything else. – Frown Mar 19 '13 at 14:23
0

I had the same issue trying to use:

net use * /delete

It always prompeted me for a Y/N and tried the solutions in here but none worked, so I'm reporting what worked for me:

net use * /delete /y

Maybe every command has a silent confirmation.

0

This worked for me when installing something from an "untrusted source":

ECHO Y | powershell Install-Module -Name PSWindowsUpdate | ECHO Y

I was trying to programmatically install the Windows Update powershell tool set :)

PDRASS
  • 1
0

For Azure CLI commands --yes is often an option.

Karson
  • 449
  • 6
  • 11
0
Disable-NetAdapter -name "Wi-Fi" -Confirm:$false

This worked for me in this scenario with PowerShell.

logi-kal
  • 7,107
  • 6
  • 31
  • 43
0

I had a similar problem but it wasn't just a yes/no option. I had some scripts that prompted for a choice of multiple options and I wanted to include them in some automated scripts. The prompts were something like this:

Select Option
What option do you want to use:
[A] Option A  [B] Option B  [C] Option C  [?] Help (default is "A"):

I found that this worked:

Echo A | powershell.exe ./script.ps1

Echo is simply an alias for 'Write-Output'. If there were more than one prompt, I could simply separate the inputs with a space like:

Echo A F G

I don't know why I needed to include the powershell.exe instead of just passing in the echo into the script.

0

Instead of putting the echo command in the front, (which will just echo all following inputs, or throw an error about the pipe) I put it at the end and that worked.

Install-Module -Name DisplaySettings | ECHO Y 
Guest87
  • 1
  • 1
0

-Confirm:$false - THIS ONE IS WORKING JUST FINE.

-1

I was trying to kill a pesky startup program and found that the Stop-Process and / or the program would not accept a piped input (no Echo Y | for me!) but I changed JPs answer a little bit: Stop-Process -name [program.name] -Force ; ran as administrator and it worked like a charm.

-1

The following Powershell worked for me:

Write-Output 'Y' | .\plink.exe <ip_address> -l <username> -pw <password>

This will output the 'Y' character to the next pipeline object.

Blake Drumm
  • 137
  • 1
  • 7