Improving my Powershell prompt with Reddit copypasta

Contrivances HerpDerp Mucking About With Things Powershell The Diary of Lupin Pooter
My Powershell prompt at the moment.
Date (as YYYYMMDD), time, and the preceding command’s run time. This is an animated GIF so that you can see how the time in the prompt updates when a command is run.

Often, when I’ve been noodling around, or installing or updating software, or trying this or that at the command line, I select all of the content in that window and save it as a text file for later reference. Sometimes, I’ve done related stuff in multiple command-line shell windows and have been switching back and forth. Occasionally, it would be useful later on to be able to know the time at which a particular operation was performed or the relative order of operations carried out in different windows. A bit of searching turned up some Powershell snippets that smooshed together in my Powershell profile file to yield the effects shown in the animated Gif above.

Here’s my Powershell Profile.ps1:

# The in-use version of this file lives at C:\Windows\System32\WindowsPowerShell\v1.0 Function Shorten-Path ([String]$Path) { $strResult = $Path.Replace($HOME, '~') # remove prefix for UNC paths $strResult = $strResult -Replace '^[^:]+::', '' # make path shorter like tabs in Vim, # handle paths starting with \\ and . correctly return ($strResult -Replace '\\(\.?)([^\\])[^\\]*(?=\\)','\$1$2') } Function Prompt { If ($IsAdmin) { $Suffix = '#' Write-Host "[ADMIN] " -NoNewline -ForegroundColor "Red" } Else { $Suffix = '$' } $host.UI.RawUI.WindowTitle = "User: " + $Env:USERNAME + " | Machine: " + $Env:COMPUTERNAME + " | IsAdmin: " + $IsAdmin $strTimestamp = "{0} " -f (Get-Date -Format "yyyyMMdd @ HH:mm:ss") Microsoft.PowerShell.Utility\Write-Host $strTimestamp -NoNewLine $LastCmd = Get-History -Count 1 if ($LastCmd) { $howlongwasthat = $LastCmd.EndExecutionTime.Subtract($LastCmd.StartExecutionTime).TotalSeconds $strHowLong = " [prev cmd exec time {0} s] " -f ($howlongwasthat) Microsoft.PowerShell.Utility\Write-Host $strHowLong -NoNewLine } Microsoft.PowerShell.Utility\Write-Host (Shorten-Path (pwd).Path) -NoNewLine -ForegroundColor "DarkBlue" -BackgroundColor "Yellow" Microsoft.PowerShell.Utility\Write-Host $Suffix -NoNewLine -ForegroundColor "DarkMagenta" -BackgroundColor "Yellow" Return " " } Set-PSReadlineKeyHandler -Chord 'Enter' ` -BriefDescription UpdatePromptAndAccept ` -LongDescription "Update the prompt to display the current time then accept the line" ` -ScriptBlock { param($key, $arg) [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() }

Most of it came from Adding Date/Time to PowerShell prompt (posted 4 years ago, so 2019-ish). First, I cribbed the whole profile, including the tidbits that embed the date and time in the prompt and shorten the current location path, from the reply by a since-deleted-account user who starts off by writing I just did something like this last week, borrowed some code from a site […]). Updating the time in the prompt when a command is run came from an answer by the user obsidianclock to a follow-up query (Now, how do I get it to act like a clock and save the time stamp after I’ve submit the command?!). I swiped the previous command’s running time stuff from the snippet posted by gangstanthony that he introduces with i like to have mine show me how long it took to run the last command.

My mashed-together copy-and-pasted-and-tweaked Powershell profile is not high-quality code and I am not suggesting or inviting you to use all or any part of it yourself but it’s good enough for my purposes for now.