Search code examples
powershellaudionotificationsbeep

How do you get Windows PowerShell to play a sound after .bat job has finished running?


As the title states, I have a .bat job running within PowerShell that when finished running, I would like a sound notification to go off. I was wondering if there was a PowerShell command that I can add to my existing PowerShell command.


Solution

  • you could use powershell automatic variables to check bat file status ..As per this,$? returns true ,if command is successfull..

    below is sample code

    $a =Invoke-Command -ScriptBlock {
    
    & "C:\temp1\test.bat"
    }
    
    if($?){
    
    [console]::beep(500,300)
    
    }
    

    You could also play custom sounds,

    $PlayWav=New-Object System.Media.SoundPlayer
    
    $PlayWav.SoundLocation=’C:\Foo\Soundfile.wav’
    
    $PlayWav.playsync()
    

    references:
    https://devblogs.microsoft.com/scripting/powertip-use-powershell-to-play-wav-files/