Announcing the End of a Script with a PowerShell Music!

5/5 - (2 votes)

This week, I was testing something for a customer that required me to check back on a data replication/sync task.  The fun part about continuously checking on something is that I’m human and I will eventually forget (or get bored and just stop doing it).  This was a time-sensitive task and I needed the answer as soon as possible. This sounds like the perfect job for a script.  But, as many of you do, I have about 30 PowerShell windows open at any time and it’s easy for stuff to get lost or forget about it.

Background

In the olden DOS days, I sometimes used a series of ECHO ^G (Control-G) statements at the end of a script to signify it was done.  I thought, for this long-running task, that type of function might be something useful here.

PowerShell does have tonal sound output capability using the [Console]::Beep(x,y) method, where is the pitch (in Hz) and y is the duration (in milliseconds).  So, ye olde internet to the rescue, and I loaded up few songs as functions in my PS Profile to use at the end of a long-running command!

Here’s an example of how I use a loop and then end with the ‘Mission Impossible’ theme:

$min = 1; while (!(Get-AzureADUser -ObjectId 9e2e3a54-f4d3-4729-acde-b04a3a2e779a).Mail) { $min = $min+1; $Date = Get-Date; "Check at $($Date), Loop $($min)"; sleep 60}; Mission-Impossible

You must be asking, ‘how can I add this to my life?’

Songs

It’s quirky and fun, which is what you’d come to expect after visiting here. I’ve dropped a few songs I’ve collected from the internet below (no credit to me for writing the music or transposing them to PowerShell beeps).

Star Wars Imperial March

Function Imperial-March
{
[console]::beep(440,500) 
[console]::beep(440,500)
[console]::beep(440,500) 
[console]::beep(349,350) 
[console]::beep(523,150) 
[console]::beep(440,500) 
[console]::beep(349,350) 
[console]::beep(523,150) 
[console]::beep(440,1000)
[console]::beep(659,500) 
[console]::beep(659,500) 
[console]::beep(659,500) 
[console]::beep(698,350) 
[console]::beep(523,150) 
[console]::beep(415,500) 
[console]::beep(349,350) 
[console]::beep(523,150) 
[console]::beep(440,1000)
}

Mission Impossible Theme

Function Mission-Impossible
{
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(932,150)
Start-Sleep -m 150
[console]::beep(1047,150)
Start-Sleep -m 150
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(699,150)
Start-Sleep -m 150
[console]::beep(740,150)
Start-Sleep -m 150
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(932,150)
Start-Sleep -m 150
[console]::beep(1047,150)
Start-Sleep -m 150
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(784,150)
Start-Sleep -m 300
[console]::beep(699,150)
Start-Sleep -m 150
[console]::beep(740,150)
Start-Sleep -m 150
[console]::beep(932,150)
[console]::beep(784,150)
[console]::beep(587,1200)
Start-Sleep -m 75
[console]::beep(932,150)
[console]::beep(784,150)
[console]::beep(554,1200)
Start-Sleep -m 75
[console]::beep(932,150)
[console]::beep(784,150)
[console]::beep(523,1200)
Start-Sleep -m 150
[console]::beep(466,150)
[console]::beep(523,150)
}

Super Mario Brothers Level 1-1 Music

Function Mario
{
[console]::beep(659,250) ##E
[console]::beep(659,250) ##E
[console]::beep(659,300) ##E
[console]::beep(523,250) ##C
[console]::beep(659,250) ##E
[console]::beep(784,300) ##G
[console]::beep(392,300) ##g
[console]::beep(523,275) ## C
[console]::beep(392,275) ##g
[console]::beep(330,275) ##e
[console]::beep(440,250) ##a
[console]::beep(494,250) ##b
[console]::beep(466,275) ##a#
[console]::beep(440,275) ##a
[console]::beep(392,275) ##g
[console]::beep(659,250) ##E
[console]::beep(784,250) ## G
[console]::beep(880,275) ## A
[console]::beep(698,275) ## F
[console]::beep(784,225) ## G
[console]::beep(659,250) ## E
[console]::beep(523,250) ## C
[console]::beep(587,225) ## D
[console]::beep(494,225) ## B
}

Tetris

Function Tetris
{
[Console]::Beep(658, 125)
[Console]::Beep(1320, 500)
[Console]::Beep(990, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 250)
[Console]::Beep(1320, 125)
[Console]::Beep(1188, 125)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 250)
[Console]::Beep(880, 500)
[Console]::Beep(880, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1320, 500)
[Console]::Beep(1188, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 750)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 500)
[Console]::Beep(1320, 500)
[Console]::Beep(1056, 500)
[Console]::Beep(880, 500)
[Console]::Beep(880, 500)
sleep -m 250 
[Console]::Beep(1188, 500)
[Console]::Beep(1408, 250)
[Console]::Beep(1760, 500)
[Console]::Beep(1584, 250)
[Console]::Beep(1408, 250)
[Console]::Beep(1320, 750)
[Console]::Beep(1056, 250)
[Console]::Beep(1320, 500)
[Console]::Beep(1188, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 500)
[Console]::Beep(990, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 500)
[Console]::Beep(1320, 500)
[Console]::Beep(1056, 500)
[Console]::Beep(880, 500)
[Console]::Beep(880, 500)
sleep -m 500 
[Console]::Beep(1320, 500)
[Console]::Beep(990, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 250)
[Console]::Beep(1320, 125)
[Console]::Beep(1188, 125)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 250)
[Console]::Beep(880, 500)
[Console]::Beep(880, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1320, 500)
[Console]::Beep(1188, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 750)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 500)
[Console]::Beep(1320, 500)
[Console]::Beep(1056, 500)
[Console]::Beep(880, 500)
[Console]::Beep(880, 500)
sleep -m 250 
[Console]::Beep(1188, 500)
[Console]::Beep(1408, 250)
[Console]::Beep(1760, 500)
[Console]::Beep(1584, 250)
[Console]::Beep(1408, 250)
[Console]::Beep(1320, 750)
[Console]::Beep(1056, 250)
[Console]::Beep(1320, 500)
[Console]::Beep(1188, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(990, 500)
[Console]::Beep(990, 250)
[Console]::Beep(1056, 250)
[Console]::Beep(1188, 500)
[Console]::Beep(1320, 500)
[Console]::Beep(1056, 500)
[Console]::Beep(880, 500)
[Console]::Beep(880, 500)
sleep -m 500 
[Console]::Beep(660, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(594, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(440, 1000)
[Console]::Beep(419, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(660, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(594, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(528, 500)
[Console]::Beep(660, 500)
[Console]::Beep(880, 1000)
[Console]::Beep(838, 2000)
[Console]::Beep(660, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(594, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(440, 1000)
[Console]::Beep(419, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(660, 1000)
[Console]::Beep(528, 1000)
[Console]::Beep(594, 1000)
[Console]::Beep(495, 1000)
[Console]::Beep(528, 500)
[Console]::Beep(660, 500)
[Console]::Beep(880, 1000)
[Console]::Beep(838, 2000)
}

Creating Your Own Music

The pitch [Console]::Beep(x,y) and duration [Console::Beep(x,y) are pretty vague values for a human to decipher and remember.  If you’re musically inclined, you may find it more useful to have a chart:

<# 
.DESCRIPTION
Musical notes for PowerShell [console]::Beep()

#>
$Note = New-Object PSObject -Property @{

<# Outside of PC speaker frequency range, for reference only
C0 = 16.35
C0s = 17.32 # C#0/Db0
D0 = 18.35
D0s = 19.45 # D#0/Eb0
E0 = 20.60
F0 = 21.83
F0s = 23.12 # F#0/Gb0
G0 = 24.50
G0s = 25.96 # G#0/Ab0
A0 = 27.50
A0s = 29.14 # A#0/Bb0
B0 = 30.87
C1 = 32.70
C1s = 34.65 # C#1/Db1
D1 = 36.71
#>
# PC Speaker frequency range begins at 37Hz
D1s = 38.89 # D#1/Eb1
E1 = 41.20
F1 = 43.65
F1s = 46.25 # F#1/Gb1
G1 = 49.00
G1s = 51.91 # G#1/Ab1
A1 = 55.00
A1s = 58.27 # A#1/Bb1
B1 = 61.74
C2 = 65.41
C2s = 69.30 # C#2/Db2
D2 = 73.42
D2s = 77.78 # D#2/Eb2
E2 = 82.41
F2 = 87.31
F2s = 92.50 # F#2/Gb2
G2 = 98.00
G2s = 103.83 # G#2/Ab24
A2 = 110.00
A2s = 116.54 # A#2/Bb2
B2 = 123.47
C3 = 130.81
C3s = 138.59 # C#3/Db3
D3 = 146.83
D3s = 155.56 # D#3/Eb3
E3 = 164.81
F3 = 174.61
F3s = 185.00 # F#3/Gb3
G3 = 196.00
G3s = 207.65 # G#3/Ab3
A3 = 220.00
A3s = 233.08 # A#3/Bb3
B3 = 246.94
C4 = 261.63 # Middle C
C4s = 277.18 # C#4/Db4
D4 = 293.66
D4s = 311.13 # D#4/Eb4
E4 = 329.63
F4 = 349.23
F4s = 369.99 # F#4/Gb4
G4 = 392.00
G4s = 415.30 # G#4/Ab4
A4 = 440.00
A4s = 466.16 # A#4/Bb4
B4 = 493.88
C5 = 523.25
C5s = 554.37 # C#5/Db5
D5 = 587.33
D5s = 622.25 # D#5/Eb5
E5 = 659.26
F5 = 698.46
F5s = 739.99 # F#5/Gb5
G5 = 783.99
G5s = 830.61 # G#5/Ab5
A5 = 880.00
A5s = 932.33 # A#5/Bb5
B5 = 987.77
C6 = 1046.50
C6s = 1108.73 # C#6/Db6
D6 = 1174.66
D6s = 1244.51 # D#6/Eb6
E6 = 1318.51
F6 = 1396.91
F6s = 1479.98 # F#6/Gb6
G6 = 1567.98
G6s = 1661.22 # G#6/Ab6
A6 = 1760.00
A6s = 1864.66 # A#6/Bb6
B6 = 1975.53
C7 = 2093.00
C7s = 2217.46 # C#7/Db7
D7 = 2349.32
D7s = 2489.02 # D#7/Eb7
E7 = 2637.02
F7 = 2793.83
F7s = 2959.96 # F#7/Gb7
G7 = 3135.96
G7s = 3322.44 # G#7/Ab7
A7 = 3520.00
A7s = 3729.31 # A#7/Bb7
B7 = 3951.07
C8 = 4186.01
C8s = 4434.92 # C#8/Db8
D8 = 4698.64
D8s = 4978.03 # D#8/Eb8
}

$Duration = New-Object PSObject -Property @{
Longa = 4000
DottedDoubleWhole = 3000
DoubleWhole = 2000
Whole = 1000
DottedHalf = 750
Half = 500
DottedQuarter = 375
Quarter = 250
DottedEighth = 187.5
Eighth = 125
DottedSixteenth = 93.75
Sixteenth = 62.5
DottedThirtySecond = 46.875
ThirtySecond = 31.25
}

Wrap it all up, and you can use it to create music like this (I transposed the Indiana Jones theme from a piece of sheet music I had).

Indiana Jones Theme Music

Function Indiana-Jones
{
[console]::Beep($Note.E4, $Duration.DottedQuarter)
[console]::Beep($Note.F4, $Duration.Eighth)
[console]::Beep($Note.G4, $Duration.Quarter)
[console]::Beep($Note.C5, $Duration.Half)
[console]::Beep($Note.D4, $Duration.DottedQuarter)
[console]::Beep($Note.E4, $Duration.Eighth)
[console]::Beep($Note.F4, $Duration.DottedHalf)
[console]::Beep($Note.G4, $Duration.DottedQuarter)
[console]::Beep($Note.A4, $Duration.Eighth)
[console]::Beep($Note.B4, $Duration.Quarter)
[console]::Beep($Note.F5, $Duration.Half)
[console]::Beep($Note.A4, $Duration.DottedQuarter)
[console]::Beep($Note.B4, $Duration.Eighth)
[console]::Beep($Note.C5, $Duration.Quarter)
[console]::Beep($Note.D5, $Duration.Quarter)
[console]::Beep($Note.E5, $Duration.Quarter)
[console]::Beep($Note.E4, $Duration.DottedQuarter)
[console]::Beep($Note.F4, $Duration.Eighth)
[console]::Beep($Note.G4, $Duration.Quarter)
[console]::Beep($Note.C5, $Duration.Half)
[console]::Beep($Note.D5, $Duration.DottedHalf)
[console]::Beep($Note.E5, $Duration.Eighth)
[console]::Beep($Note.F5, $Duration.DottedHalf)
[console]::Beep($Note.G4, $Duration.DottedQuarter)
[console]::Beep($Note.G4, $Duration.DottedEighth)
[console]::Beep($Note.E5, $Duration.Quarter)
[console]::Beep($Note.D5, $Duration.DottedQuarter)
[console]::Beep($Note.G4, $Duration.Eighth)
[console]::Beep($Note.E5, $Duration.Quarter)
[console]::Beep($Note.D5, $Duration.DottedQuarter)
[console]::Beep($Note.G4, $Duration.Eighth)
[console]::Beep($Note.E5, $Duration.Quarter)
[console]::Beep($Note.D5, $Duration.DottedQuarter)
[console]::Beep($Note.G4, $Duration.Eighth)
[console]::Beep($Note.E5, $Duration.Quarter)
[console]::Beep($Note.D5, $Duration.Quarter)
[console]::Beep($Note.G4, $Duration.Eighth)
[console]::Beep($Note.F5, $Duration.Quarter)
[console]::Beep($Note.E5, $Duration.Eighth)
[console]::Beep($Note.D5, $Duration.Eighth)
[console]::Beep($Note.C5, $Duration.Whole) 
}

It will take some trial and error, because [console]:Beep() may not respond quickly enough for you, but it’s definitely a good way to spend some down time tinkering!

May your days be filled with low fidelity music!  Convert some of your favorite songs and share them with us!