Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Short description
Explains how to customize how PowerShell reads input at the console prompt.
Long description
Starting in Windows PowerShell 3.0, you can write a function named
PSConsoleHostReadLine
that overrides the default way that console input is
processed.
This function is extended by the PSReadLine module.
Examples
The following example launches Notepad and gets input from a text file that the user creates:
function PSConsoleHostReadLine {
$inputFile = Join-Path $Env:TEMP PSConsoleHostReadLine
Set-Content $inputFile "PS > "
# Notepad opens. Enter your command in it, save the file, and then exit.
notepad $inputFile | Out-Null
$userInput = Get-Content $inputFile
$resultingCommand = $userInput.Replace("PS >", "")
$resultingCommand
}
Remarks
By default, PowerShell reads input from the console in what is known as "Cooked
Mode" -- in which the Windows console subsystem handles all the keypresses,
F7 menus, and other input. When you press Enter or
Tab, PowerShell gets the text that you have typed up to that point.
There is no way for it to know that you pressed Ctrl+R,
Ctrl+A, Ctrl+E, or any other keys
before pressing Enter or Tab. In Windows PowerShell 3.0,
the PSConsoleHostReadLine
function solves this issue. When you define a
function named PSConsoleHostReadline
in the PowerShell console host,
PowerShell calls that function instead of the "Cooked Mode" input mechanism.