Scott Hanselman

You should be customizing your PowerShell Prompt with PSReadLine

December 08, 2020 Comment on this post [7] Posted in PowerShell
Sponsored By

I use PowerShell 7 (cross platform, open source, runs on .NET Core) as my main shell of choice at the Windows command line. I use it in the Windows Terminal and I have a pretty prompt thanks to OhMyPosh. I've also set up autocomplete in PowerShell (type something, then TAB) with git and dotnet!

Do make sure you know the difference between a console, terminal, and shell.

PowerShell baby

I thought I had things pretty well dialed in. I even used PSReadLine, a bash inspired readline implementation for PowerShell.

But was I REALLY USING IT? No. Honestly, at the time I wouldn't be able to tell you what it offered me as a command line users. Wow was I wrong.

Don't sleep on PSReadLine if you use PowerShell as your preferred prompt. Head over there and give them some love and a star and buckle up, friends!

Head over to a prompt and run

Install-Module PSReadLine -AllowPrerelease -Force

If you want the latest, otherwise remove the Prerelease. Then edit your $profile. I usually do this:

notepad $PROFILE

And add

if ($host.Name -eq 'ConsoleHost')
Import-Module PSReadLine

First, PSReadLine makes everything better with sprinkles of color everywhere automatically. But the thing I was not really digging into was customizing my $profile to light up cool features and set keybindings that made sense to me.


It was this totally configured and blinged out sample PSReadline Profile that made me realize I wasn't doing enough. I cherry-picked the best parts out of this and I recommend you do the same!

You get nice improvements with bash-like command line editing. The most important one being the PowerShell equivalent of ctrl-r "bck-i-search" that bash users enjoy.

You can also set command line handlers so pressing "up arrow" with some existing text will find that item in history. Set it up once in your $profile.

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

Like Emacs but want PowerShell to be more like Emacs? It's possible!

Set-PSReadLineOption -EditMode Emacs

Got PSReadLine already but you're not sure what is key bindings are set up today?


This will give you a complete picture. You know how you can "ctrl shift b" in Visual Studio to build your project? I made ctrl shift b type "dotnet build" for me in PowerShell! Muscle memory for the win!

# This is an example of a macro that you might use to execute a command.
# This will add the command to history.
Set-PSReadLineKeyHandler -Key Ctrl+Shift+b `
-BriefDescription BuildCurrentDirectory `
-LongDescription "Build the current directory" `
-ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("dotnet build")

Check out the really advanced stuff in the sample $profile. I like the Smart Insert/Delete. It will take over your ' quotes and quotes " and braces { and ) parens and make them auto matching! That means you can type a variable or string, select it with Shift Arrow Key or Ctrl Shift Arrow Key, then typeover it with a " and it'll automatically quote the whole string. A very nice command line experience that you may enjoy in other shells but you never really set up PowerShell. Even more useful is Alt+' that will take a string and change it from a 'string' to a "string."

Take a few moments and think about the things you type too often. The things you type twice, or ten times a day. Check out your $profile, consider your aliases, and tidy up. I suspect PSReadLine could help. It was great for me!

While you are at it, also pick up PSColors! Next post I'll talk about Oh my Posh 3!

Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
December 10, 2020 18:31
Wonderful, but you've got a typo:

Set-PSReadLineOption -EditMode vi
December 10, 2020 20:04
PowerShell 5 used to update the host window title with each prompt automatically to be the current working folder, we can override the prompt function in PowerShell 7 to do that, but using oh-my-posh overrides the override!

So I would edit oh-my-posh\2.0.487\oh-my-posh.psm1 on line 29:

#set window title to current folder
$host.ui.RawUI.WindowTitle = Get-Location | Split-Path -leaf

#prevent altering the title
$location = Get-Location | Split-Path -leaf
December 11, 2020 4:22
I beleive PSReadline is automatically loaded without need for profile modification. You can check if it's loaded by executing Get-Module.
December 11, 2020 12:40
Just wondering whether a closer look at PowerShell would be a good topic for your Computer Stuff YouTube videos? I use Windows Cmd and Bash but have never got to grips with PowerShell. I'm a C++ developer mainly, and books on PS tend to be from a sys admin perspective. Perhaps you could introduce it from the perspective of a developer?
December 11, 2020 13:10
Thanks Scott. Really appreciate these 'little' posts about productivity. Often we (well I) take things for granted and just keep using them in the same way without realizing how much more they can do. Thanks!
December 11, 2020 15:26
Thank you for the post!

Just wanted to point out that your terminal's theme is now very similar to oh-my-zsh's agnoster theme:
December 12, 2020 19:48
Writing a PowerShell script can be done with a tool as simple as Notepad, However you will agree that it is not the most efficient tool because it does not allow for example to debug this script or to benefit from syntax highlighting which is welcome in any development environment.

As soon as PowerShell was released, Microsoft integrated a script editor into Windows: PowerShell ISE (Integrated Scripting Environment). This environment allows you to write PowerShell scripts while benefiting from many features:

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.