Skip to content

Instantly share code, notes, and snippets.

Last active August 15, 2023 08:02
Show Gist options
  • Save devblackops/2dea8440b48b4d378f220841c07ec2a2 to your computer and use it in GitHub Desktop.
Save devblackops/2dea8440b48b4d378f220841c07ec2a2 to your computer and use it in GitHub Desktop.
Display highly visible notification if the last command failed in the Microsoft Terminal using PowerShell
# Put this code in your PowerShell profile script
# This requires the MSTerminalSettings module which you can download with:
# Install-Module MSTerminalSettings -Scope CurrentUser -Repository PSGallery
Import-Module MSTerminalSettings
$msTermProfileName = 'pwsh' # Replace with whatever Terminal profile name you're using
$msTermProfile = Get-MSTerminalProfile -Name $msTermProfileName
$script:bombThrown = $false
function prompt {
if ($? -eq $false) {
# Only do this if we're using Microsoft Terminal
if ((Get-Process -Id $PID).Parent.Parent.ProcessName -eq 'WindowsTerminal') {
Set-MSTerminalProfile -Name $ -BackgroundImage '' -UseAcrylic:$false
$script:bombThrown = $true
} else {
# Reset to previous settings
if ($script:bombThrown) {
Set-MSTerminalProfile -Name $ -BackgroundImage $msTermProfile.backgroundImage -UseAcrylic:$msTermProfile.useAcrylic
$script:bombThrown = $false
Copy link

gpduck commented Jun 29, 2019

Great idea, this is fun :)

Set-MSTerminalProfile -Name pwsh -CommandLine "pwsh -noexit -command `$env:WT_PROFILE = 'pwsh'"
Set-MSTerminalProfile -Name powershell -CommandLine "powershell -noexit -command `$env:WT_PROFILE = 'powershell'"

function prompt {
  $IsFail = !$?
  if(!$Global:__PromptPS) {
    $Runspace = [runspacefactory]::CreateRunspace()
    $Global:__PromptPS = [PowerShell]::Create()
    $Global:__PromptPS.Runspace = $Runspace
      ipmo msterminal
      $OldProfile = Get-MSTerminalProfile -Name $env:WT_PROFILE
      Set-MSTerminalProfile -name $env:WT_PROFILE -BackgroundImage '' -backgroundImageStretchMode uniformToFill -UseAcrylic:$False
      Start-Sleep -Seconds 2.41
      $OldProfile | Set-MSTerminalProfile
  if($IsFail) {
  "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
# Use this to reset the runspace if you update the function
$global:__PromptPS.Dispose(); $global:__PromptPS = $null

Copy link

For bonus notification visibility, you could add this function and point it at an appropriate sound when you error.

Function Play-Sound{
    $player = New-Object System.Media.SoundPlayer $SoundFile

Copy link

You might consider adding a blowoff valve for robocopy:

The return code from Robocopy is a bitmap, defined as follows:

    Hex   Decimal  Meaning if set

    0×00   0       No errors occurred, and no copying was done.
                   The source and destination directory trees are completely synchronized. 

    0×01   1       One or more files were copied successfully (that is, new files have arrived).

    0×02   2       Some Extra files or directories were detected. No files were copied
                   Examine the output log for details. 

    0×04   4       Some Mismatched files or directories were detected.
                   Examine the output log. Housekeeping might be required.

    0×08   8       Some files or directories could not be copied
                   (copy errors occurred and the retry limit was exceeded).
                   Check these errors further.

    0×10  16       Serious error. Robocopy did not copy any files.
                   Either a usage error or an error due to insufficient access privileges
                   on the source or destination directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment