Skip to content

Instantly share code, notes, and snippets.

@asheroto
Forked from murven/play-sound.ps1
Last active October 18, 2021 04:19
Show Gist options
  • Save asheroto/63e56848eae5af3b7d670a9a7014eee7 to your computer and use it in GitHub Desktop.
Save asheroto/63e56848eae5af3b7d670a9a7014eee7 to your computer and use it in GitHub Desktop.
Play sound in PowerShell
# Native sound player (wav only)
$player = New-Object System.Media.SoundPlayer "$env:windir\Media\notify.wav"
$player.Play()
# Windows Media Player (mp3, wav, any common format)
Add-Type -AssemblyName presentationCore
$mediaPlayer = New-Object System.Windows.Media.MediaPlayer
$mediaPlayer.open("C:\Path\To\Your\Sound.mp3")
# Give time to load the file
Start-Sleep -Seconds 1
# Set duration of sound to var
$duration = $mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds
# Play sound
$mediaPlayer.Play()
# Wait until duration has elapsed
Start-Sleep -Seconds $duration
# Stop and close
$mediaPlayer.Stop()
$mediaPlayer.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment