Open MsgBox with Random Sound

by Apr 28, 2014

You may have seen script code that opens a MsgBox dialog box. Today, you get a piece of code that opens a MsgBox and plays a random sound, adding extra attention and fun. The sound stops when the user responds to the MsgBox:

# find random WAV file in your Windows folder
$randomWAV = Get-ChildItem -Path C:\Windows\Media -Filter *.wav | 
  Get-Random |
  Select-Object -ExpandProperty Fullname

# load Forms assembly to get a MsgBox dialog
Add-Type -AssemblyName System.Windows.Forms

# play random sound until MsgBox is closed
$player = New-Object Media.SoundPlayer $randomWAV
$result = [System.Windows.Forms.MessageBox]::Show("We will reboot your machine now. Ok?", "PowerShell", "YesNo", "Exclamation")

Twitter This Tip! ReTweet this Tip!