Leverage PowerShell History across multiple machines
Hello Folks,
If you have not seen the content our team has put together for our “ITOpsTalks: All things Hybrid” online event? You can still watch all the sessions online. To the point of today’s post. During the OPS117: PowerShell Deep dive session Joey Aiello and Jason Helmick introduced us to Predictive IntelliSense otherwise known as PSReadLine.
PSReadLine replaces the command line editing experience of PowerShell PSReadLine and up. It provides:
- Syntax coloring.
- Simple syntax error notification.
- A good multi-line experience (both editing and history).
- Customizable key bindings.
- Cmd and emacs modes (neither are fully implemented yet, but both are usable).
- Many configuration options.
- Bash style completion (optional in Cmd mode, default in Emacs mode).
- Bash/zsh style interactive history search (CTRL-R).
- Emacs yank/kill ring.
- PowerShell token based "word" movement and kill.
- Undo/redo.
- Automatic saving of history, including sharing history across live sessions.
- "Menu" completion (somewhat like Intellisense, select completion with arrows) via Ctrl+Space.
The "out of box" experience is meant to be very familiar to PowerShell users with no need to learn any new keystrokes.
I really think this is a great new editing experience for anyone working with PowerShell scripts but as I mentioned to Joey during our talk “is there a way to share the PowerShell console history across multiple machines. Jason mentioned that you can pick up the history files and move it around.
I ran a little experiment and it’s working flawlessly. So far… LOL
Preparing Machine 1
Before we start. I use the latest version of PowerShell 7 on all my machines. Yes Windows PowerShell is installed but I did NOT try the following experiment with Windows Powershell. If you are planning on trying this on Windows PowerShell. You will need the 1.6.0 or a higher version of PowerShellGet to install the latest prerelease version of PSReadLine.
Windows PowerShell 5.1 ships an older version of PowerShellGet which doesn't support installing prerelease modules, so Windows PowerShell users need to install the latest PowerShellGet by running the following commands from an elevated Windows PowerShell session.
So, once I had PSReadLine installed on my PC I verified the location of my history file by using the Get-PSReadLineOption command.
I copied the “ConsoleHost_history.txt” file to a folder I created in my OneDrive folder.
In my case “D:\1drv\OneDrive\PS-Hystory”. You can put it wherever you want. After copying the file all I needed to do is set the location by using the following command:
Set-PSReadLineOption -HistorySavePath D:\1drv\OneDrive\PS-Hystory\ConsoleHost_history.txt
Preparing Machine 2
I moved to one of the laptops I use when I travel (which I have not done in a long time), I installed the module,
Install-Module -Name PSReadLine -AllowPrerelease
imported it,
Import-Module PSReadLine
Used the Set-PSReadLineOption cmdlet to customizes the behavior of the PSReadLine module to use the history file.
Set-PSReadLineOption -PredictionSource History
And checked my settings.
Finalizing settings
Just to ensure that the folder on all my machines would always be updated I set the properties on the folder to “Always keep on this device” on all my machines.
Now I have a shared history file on all my PCs. This was fun and will be useful.
Cheers!
Pierre