I'm having a hard time to trying to auto-fill the Read-Host input in PowerShell

I have a script that reads files, extracts lines and store them in a CSV file for fruther changes.

I'd like to be able to change each line on demand from the Powershell window without having to re-type the entire line.

  1. Read the file
  2. Find the next available line
  3. Make the line the Read-Host input
  4. User would then use the arrow keys to move the cursor and make changes to the line
  5. Press Enter to save the line to a new file

"_" in the below example of the desired state of Read-Host is the cursor blinking ready to makechanges*

#> Line to change : server=servername.loc.corporation_

Here is a basic approach to get you started. You should really be including the code you're trying and specific examples of where you're getting stuck.

The below will output one line at a time, then ask the user to input whatever they want. This input can be a replication of the original line, or any changes they wish to make.

$sourcefile = gc "file1.txt"
$newfile = "file2.txt"

foreach ($line in $sourcefile) {
     Read-Host "Edit this line -" $line | Out-File -Append $newfile
  • So people will still have to rewrite the whole line instead of editing pre-filled ? That's my only problem, the rest of the script works well. – JeanneD4RK Aug 18 '16 at 4:08
  • If this doesn't help you get started, then post your code and tell exactly where you're having problems. – root Aug 18 '16 at 11:01

[System.Windows.Forms.SendKeys]::SendWait("YourText") before Read-Host will help you. But it work only if console window has focus...

  • 1
    I don't even remember asking that – JeanneD4RK Oct 24 '18 at 12:19
  • It was for my previous company, we can dump this thread – JeanneD4RK Oct 24 '18 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.