Hey there.
I'm gonna give a simple trick to make your life easier when running time consuming commands in PowerShell 7.
So a while back I installed Arch Linux to see what the hype was about, I opened Konsole and noticed a notification pop up when the command I ran finished.
After a while I reinstalled Windows, and wanted to make that notification functionality work in PS7.
How this works?
This works by recording a timestamp when Enter is pressed and comparing it when PowerShell renders the next prompt. If execution time exceeds a threshold, a Windows toast notification is triggered. That's it.
If you’re hooked, Let’s make this happen.
Step 1
Install PS7 module by running:
> Install-Module BurntToast
Step 2
Run the following in PS7 to get the PS7 Profile path:
> $PROFILE
C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Step 3
Open your profile in VS Code or whatever editor you use.
> code $PROFILE
Step 4
Import the Module you just installed
Import-Module BurntToast
- This should always be at the top, otherwise the next steps won't work
Step 5
Copy and Paste this code at the top of your profile after importing the module
$OriginalPrompt = (Get-Command prompt).ScriptBlock # capture original prompt
# Notification need a logo to use, so we'll be using PowerShell's icon
$applogo = "C:\Program Files\PowerShell\7\assets\Powershell_av_colors.png"
$ExecutionNotifyThreshold = 3 # seconds
$global:__commandStart = $null
# To format timestamp as human readable as possible
function Format-Elapsed([TimeSpan]$timestamp) {
if ($timestamp.TotalSeconds -lt 60) {
return ('{0:0.0}s' -f $timestamp.TotalSeconds)
}
$parts = New-Object System.Collections.Generic.List[string]
if ($timestamp.Days -gt 0) { $parts.Add("{0}d" -f $timestamp.Days) }
if ($timestamp.Hours -gt 0) { $parts.Add("{0}h" -f $timestamp.Hours) }
if ($timestamp.Minutes -gt 0) { $parts.Add("{0}m" -f $timestamp.Minutes) }
$remainingSeconds = [math]::Floor($timestamp.TotalSeconds % 60)
$parts.Add("{0}s" -f $remainingSeconds)
return ($parts -join ' ')
}
function global:prompt {
if ($global:__commandStart) {
$elapsed = (Get-Date) - $global:__commandStart
if ($elapsed.TotalSeconds -ge $ExecutionNotifyThreshold) {
$lastCmd = (Get-History -Count 1).CommandLine
$text = @(
'PS7 Finished:'
"`$ $lastCmd"
("Took {0}" -f (Format-Elapsed $elapsed))
)
New-BurntToastNotification -Text $text -AppLogo $applogo
}
$global:__commandStart = $null
}
& $OriginalPrompt
}
$ExecutionNotifyThreshold is the time window you want to be notified when the command is taking longer than this threshold. Adjust as you wish
We have one last step and we are done, keep going 😁🚀
Step 6
Now add the below code at the end of your $PROFILE
This should always be at the end, so nothing else will override your custom functionality
Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
$global:__commandStart = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Now Save the file, restart your shell (you can also run ". $PROFILE" to restart PS7)
run a command that will take longer than your threshold, you should see a similar Notification pop up like this:
Notes & Caveats
I have been using oh-my-posh with this approach for a year now and had no problems so far
This overrides the global
promptfunction. If you use oh-my-posh, starship, or another prompt framework, load this after your prompt is configured.This hooks the
Enterkey via PSReadLine. If you already have a custom Enter handler, you’ll need to merge the logic.To remove the behavior:
Remove-PSReadLineKeyHandler -Key Enter
That's it! you're Done. Congrats 🥳
This removes the need to babysit long-running commands and keeps you informed without breaking flow.
Tell me what you think about this approach. ❤️

Top comments (0)