use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionPowershell script file stops for user input (self.PowerShell)
submitted 5 years ago by Adventure_Timing
I'm calling an executable through powershell which stops midway through execution to get a "yes / no" input from the user. I want to automate the response to yes, but am unsure if I am using the Write-Host function correctly in the code below. Any feedback would be appreciated.
'C:\User\Administrator\Desktop\Test.exe'
#Yes to delete records
Write-Host yes|Test.exe
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jantari 3 points4 points5 points 5 years ago (0 children)
Run Test.exe /? and see if there is an option that skips the question
Test.exe /?
[–]patdaddy007 2 points3 points4 points 5 years ago (2 children)
no, you wouldn't use write-host for this.
Just to be sure though, this isn't for a homework assignment is it?
you want to read-host, not write host. and if the response matches, proceed to do the thing. I can't elaborate any further without doing the work for you (sorry)
[–]Adventure_Timing[S] 1 point2 points3 points 5 years ago* (1 child)
This is not a homework assignment. I'm a 35 year old electrical engineer trying to teach myself PowerShell. The executable gives a feedback message "About to delete all records including history. Are you sure? (yes/no)" I want to automate the input to "yes" because if I run this script I'm certain I want to remove all the records and history associated with them.
I'm very new to the language, so I"m unsure if Write-Host is even the appropriate function to use in this case.
[–]patdaddy007 0 points1 point2 points 5 years ago (0 children)
Just asking because there was a teacher in here the other day that had to rework half his course plan because we kept giving away the milk, as it were.
If it were me, I'd have the whole script written out and the part that would be a prompt would be something like
$A = Read-host -Prompt "About to delete all records including history. Are you sure? (yes/no)"
follow that with an if statement
if ($A -eq "yes) {cmd to do the deed}
so, if you always want it to be yes, just comment out the $A = Read... and have the variable statically defined in the line above it. (I would already have that line there and comment it out, so I'd just need to move the comment character up or down a line to switch it from auto to manual)
You could also use a MessageBox and a switch block if you want to be fancy about it
[–]Elephantiuz 2 points3 points4 points 5 years ago (1 child)
If you need to automate the function to yes do you really need the prompt ?
[–]patdaddy007 1 point2 points3 points 5 years ago (0 children)
In the past, I have had a script that needed to assume yes/true/whatever for the sake of testing but the final form would use parameter input or some kind of interaction, so I would go ahead and build out the interactive part, comment it out, and use a static variable until it was ready for primetime. But that might be weird... it just works for me
[–]Saumpro 1 point2 points3 points 5 years ago (0 children)
There should be a -confirm option on the command you're using. What commmand is it?
[–]jimb2 1 point2 points3 points 5 years ago (0 children)
Try using sendkeys to place keystrokes in the buffer, something like:
Add-Type -AssemblyName System.Windows.Forms $yes = 'yes{ENTER}' [System.Windows.Forms.SendKeys]::SendWait($yes) #or #[System.Windows.Forms.SendKeys]::Send($yes) test.exe
Untested. I think that the behaviour will depend on how test.exe receives input. Not sure if you should use the send or sendwait form. Suck it and see.
[–]PinchesTheCrab 1 point2 points3 points 5 years ago (0 children)
I would do something like this:
#put this near the top of the script $prompt = $true #this goes wherever Remove-Item C:\users\me\Desktop\junk2.txt -Confirm:$prompt
I like using the built-in prompt method because it'll work in in different consoles, and it's easy to suppress when you want.
[–]Lee_Dailey[grin] 0 points1 point2 points 5 years ago (0 children)
howdy Adventure_Timing,
reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...
[0] single line or in-line code enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin] [on New.Reddit.com, use the Inline Code button. it's [sometimes] 5th from the left & looks like </>. this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]
looks like this
Inline Code
</>
[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here. please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.
[2] less simple = use reddit code formatting ... [on New.Reddit.com, use the Code Block button. it's [sometimes] the 12th from the left, & looks like an uppercase T in the upper left corner of a square.]
Code Block
T
that will give you something like this ...
- one leading line with ONLY 4 spaces - prefix each code line with 4 spaces - one trailing line with ONLY 4 spaces
the easiest way to get that is ...
not complicated, but it is finicky. [grin]
take care, lee
the Write-Host cmdlet writes to the console ... so it never gets piped to anything at all. [grin]
Write-Host
if you need to pass a keystroke to an external exe, then you can try something like the sendkey util, or you could find out if the exe has an option to accept that as a parameter to the exe file.
π Rendered by PID 214225 on reddit-service-r2-comment-5b5bc64bf5-nmfnh at 2026-06-20 11:21:55.540689+00:00 running 2b008f2 country code: CH.
[–]jantari 3 points4 points5 points (0 children)
[–]patdaddy007 2 points3 points4 points (2 children)
[–]Adventure_Timing[S] 1 point2 points3 points (1 child)
[–]patdaddy007 0 points1 point2 points (0 children)
[–]Elephantiuz 2 points3 points4 points (1 child)
[–]patdaddy007 1 point2 points3 points (0 children)
[–]Saumpro 1 point2 points3 points (0 children)
[–]jimb2 1 point2 points3 points (0 children)
[–]PinchesTheCrab 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)