I'm new to Powershell. I'm trying to create my own custom prompt that shows a truncated version of the current dir unless you expand it by pressing CTRL + R
. Moreover, I would like to do this:
$short\dir\path...>
*user presses CTRL+R*
$really\really\long\full\dir\path>
*Full dir path is shown as long as CTLR + R is held down*
The tilde (~) represents the ENTER keystroke. You can also use {ENTER} , though they're not identical - that's the keypad's ENTER key.
Use Read-Host to Enable the press any key to continue in the PowerShell. Read-Host is the most common method to prompt user input. You can use this method to pause the execution when prompting user input in the PowerShell. After pressing the key, you need to press Enter to exit the pause mode.
This isn't a super elegant solution, but it basically turns Ctrl + Alt + R into a hotkey for displaying the current directory. You can change the chord to "Ctrl+R" but you'll be overwriting a default PSReadlineKeyHandler for powershell. It looks like it gets stuck on the command, but if you begin typing the next line will appear. You will need to add both of these to your Microsoft.Powershell_Profile.ps1
located in $env:USERPROFILE\Documents\WindowsPowershell\
function prompt {
if(($pwd | split-path -Parent | Split-Path -Leaf) -match ':')
{
"$pwd\>"
}
elseif (($pwd | Split-Path -Leaf) -match ':')
{
"$pwd>"
}
else {
"...\$($pwd | split-path -Parent | Split-Path -Leaf)\$($pwd | Split-path -Leaf)\>"
}
}
Set-PSReadlineKeyHandler -Chord "Ctrl+Alt+R" -ScriptBlock {
write-host $pwd -nonewLine
}
Hopefully that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With