Search notes:

System.Windows.Media.MediaPlayer (class)

Play sound files in PowerShell

The following simple PowerShell example plays some mp3 files that are located under C:\Windows\Media.
set-strictMode -version latest

add-type -assemblyName presentationCore

$mediaPlayer = new-object system.windows.media.mediaplayer

function play-soundFile {
   param( $file )

   $length_ms = 0
   $mediaPlayer.Open($file)
   do {

      try {
           $length_ms = $mediaPlayer.NaturalDuration.TimeSpan.TotalMilliseconds
      }
      catch [System.Management.Automation.PropertyNotFoundException] {
         start-sleep -milliSecond 10
      }
   }
   until ($length_ms)

  "Playing $file, duration $length_ms ms"
   $mediaPlayer.Play()
   start-sleep -milliSecond $length_ms

   $mediaPlayer.Stop()
   $mediaPlayer.Close()

}

play-soundFile 'C:\WINDOWS\media\Windows Foreground.wav'
play-soundFile 'C:\WINDOWS\media\Windows Background.wav'
play-soundFile 'C:\WINDOWS\media\Windows Logon.wav'
Github repository .NET-API, path: /System/Windows/Media/MediaPlayer/play-sound-files.ps1

Index