Skip to content

Happy Star Wars Day! How to play the Star Wars main theme in PowerShell

company JJB
JJ Bateman|May 4, 2017
Star Wars PowerShell
Star Wars PowerShell

It’s the age-old* question people have been asking since at least 2006. Which is nerdier, Star Wars or PowerShell? While the debate rages on, we’ve taken on a different task. How can we join the two together for the ultimate mashup of fandom meets technical prowess?
While it may be an unconventional use of PDQ Deploy (running a non-silent pun intended interactive windowed deployment), it’s still a fun experiment.

Loading...

Behind the Scenes

Helpful cmdlets in this post:

Here is an excerpt of the trumpet part. Copy it into your PowerShell ISE and give it a run.

$sw = [Diagnostics.Stopwatch]::StartNew() $host.ui.RawUI.BackgroundColor = "Blue" $host.ui.RawUI.ForegroundColor = "White" function Pause-Console { param([int]$p = 0,[int]$n = 0) $playhead = $p $next = $n $savedColor = $host.ui.RawUI.BackgroundColor $elapsed = $sw.elapsedMilliseconds $pelapsed = $elapsed - $playhead $dur = $next-$playhead if($pelapsed -lt 0) { $pos = [math]::abs($pelapsed) + $dur if($pos -gt 200) { $host.ui.RawUI.BackgroundColor = "Black" Clear-Host } Write-Output "Start-Sleep -m $pos" Start-Sleep -m $pos if($pos -gt 200) { $host.ui.RawUI.BackgroundColor = $savedColor Clear-Host } }else{ $dif = $dur - $pelapsed if($dif -gt 0) { if($dif -gt 200) { $host.ui.RawUI.BackgroundColor = "Black" Clear-Host } Write-Output "Start-Sleep -m $dif" Start-Sleep -m $dif if($dif -gt 200) { $host.ui.RawUI.BackgroundColor = $savedColor Clear-Host } } } } function Console-Beep { param( [int]$h = 0, [int]$p = 0, [int]$n = 0) $hertz = $h $playhead = $p $next = $n $elapsed = $sw.elapsedMilliseconds $pelapsed = $elapsed - $playhead $dur = $next-$playhead if($pelapsed -lt 0) { $pos = [math]::abs($pelapsed) $dur = $dur+$pos Write-Output "[console]::beep($hertz,$dur)" [console]::beep($hertz,$dur) }else{ $dif = $dur - $pelapsed if($dif -gt 0) { Write-Output "[console]::beep($hertz,$dif)" [console]::beep($hertz,$dif) } } } Console-Beep -h 880 -p 566 -n 778 Pause-Console -p 778 -n 1132 Console-Beep -h 880 -p 1132 -n 1344 Pause-Console -p 1344 -n 1698 Console-Beep -h 880 -p 1698 -n 1910 Pause-Console -p 1910 -n 4867 Console-Beep -h 466 -p 4867 -n 4909 Pause-Console -p 4909 -n 4926 Console-Beep -h 466 -p 4926 -n 4968 Pause-Console -p 4968 -n 4989 Console-Beep -h 466 -p 4989 -n 5029 Pause-Console -p 5029 -n 5051 Console-Beep -h 466 -p 5051 -n 5107 Pause-Console -p 5107 -n 5119 Console-Beep -h 466 -p 5119 -n 5197 Pause-Console -p 5197 -n 5281 Console-Beep -h 349 -p 5281 -n 5338 Pause-Console -p 5338 -n 5449 Console-Beep -h 622 -p 5449 -n 5515 Pause-Console -p 5515 -n 5664 Console-Beep -h 698 -p 5664 -n 5742 Pause-Console -p 5742 -n 5864 Console-Beep -h 622 -p 5864 -n 5932 Pause-Console -p 5932 -n 6055 Console-Beep -h 466 -p 6055 -n 6140 Pause-Console -p 6140 -n 6270 Console-Beep -h 466 -p 6270 -n 6355 Pause-Console -p 6355 -n 6461 Console-Beep -h 349 -p 6461 -n 6540 Pause-Console -p 6540 -n 6661 Console-Beep -h 622 -p 6661 -n 6721 Pause-Console -p 6721 -n 6874 Console-Beep -h 698 -p 6874 -n 6946 Pause-Console -p 6946 -n 7074 Console-Beep -h 622 -p 7074 -n 7134 Pause-Console -p 7134 -n 7261 Console-Beep -h 466 -p 7261 -n 7330 Pause-Console -p 7330 -n 7470 Console-Beep -h 466 -p 7470 -n 7545 Pause-Console -p 7545 -n 7670 Console-Beep -h 349 -p 7670 -n 7751 Pause-Console -p 7751 -n 7874 Console-Beep -h 622 -p 7874 -n 7946 Pause-Console -p 7946 -n 8082 Console-Beep -h 698 -p 8082 -n 8155 Pause-Console -p 8155 -n 8311 Console-Beep -h 466 -p 8311 -n 8377 Pause-Console -p 8377 -n 8389 Console-Beep -h 466 -p 8389 -n 8482 Pause-Console -p 8482 -n 8545 Console-Beep -h 466 -p 8545 -n 8639 Pause-Console -p 8639 -n 8832 Console-Beep -h 349 -p 8832 -n 8995 Pause-Console -p 8995 -n 9151 Console-Beep -h 349 -p 9151 -n 9295 Pause-Console -p 9295 -n 9470 Console-Beep -h 349 -p 9470 -n 9626 Pause-Console -p 9626 -n 9882 Console-Beep -h 466 -p 9882 -n 11051 Pause-Console -p 11051 -n 11114 Console-Beep -h 698 -p 11114 -n 12116 Pause-Console -p 12116 -n 12213 Console-Beep -h 622 -p 12213 -n 12276 Pause-Console -p 12276 -n 12343 Console-Beep -h 587 -p 12343 -n 12424 Pause-Console -p 12424 -n 12491 Console-Beep -h 523 -p 12491 -n 12554 Pause-Console -p 12554 -n 12662 Console-Beep -h 932 -p 12662 -n 13709 Pause-Console -p 13709 -n 13769 Console-Beep -h 698 -p 13769 -n 14238 Pause-Console -p 14238 -n 14353 Console-Beep -h 622 -p 14353 -n 14444 Pause-Console -p 14444 -n 14528 Console-Beep -h 587 -p 14528 -n 14638 Pause-Console -p 14638 -n 14734 Console-Beep -h 523 -p 14734 -n 14821 Pause-Console -p 14821 -n 14959 Console-Beep -h 932 -p 14959 -n 16144 Pause-Console -p 16144 -n 16171 Console-Beep -h 698 -p 16171 -n 16646 Pause-Console -p 16646 -n 16788 Console-Beep -h 622 -p 16788 -n 16885 Pause-Console -p 16885 -n 16963 Console-Beep -h 587 -p 16963 -n 17063 Pause-Console -p 17063 -n 17163 Console-Beep -h 622 -p 17163 -n 17260 Pause-Console -p 17260 -n 17369 Console-Beep -h 523 -p 17369 -n 18723 Pause-Console -p 18723 -n 18856 Console-Beep -h 349 -p 18856 -n 18913 Pause-Console -p 18913 -n 18950 Console-Beep -h 349 -p 18950 -n 19025 Pause-Console -p 19025 -n 19050 Console-Beep -h 349 -p 19050 -n 19125 Pause-Console -p 19125 -n 19165 Console-Beep -h 466 -p 19165 -n 20359 Pause-Console -p 20359 -n 20378 Console-Beep -h 698 -p 20378 -n 21528 Pause-Console -p 21528 -n 21578 Console-Beep -h 622 -p 21578 -n 21663 Pause-Console -p 21663 -n 21763 Console-Beep -h 587 -p 21763 -n 21848 Pause-Console -p 21848 -n 21959 Console-Beep -h 523 -p 21959 -n 22065 Pause-Console -p 22065 -n 22181 Console-Beep -h 932 -p 22181 -n 23354 Pause-Console -p 23354 -n 23381 Console-Beep -h 698 -p 23381 -n 23960 Pause-Console -p 23960 -n 23975 Console-Beep -h 622 -p 23975 -n 24075 Pause-Console -p 24075 -n 24175 Console-Beep -h 587 -p 24175 -n 24248 Pause-Console -p 24248 -n 24359 Console-Beep -h 523 -p 24359 -n 24434 Pause-Console -p 24434 -n 24571 Console-Beep -h 932 -p 24571 -n 25753 Pause-Console -p 25753 -n 25771 Console-Beep -h 698 -p 25771 -n 26334 Pause-Console -p 26334 -n 26375 Console-Beep -h 622 -p 26375 -n 26475 Pause-Console -p 26475 -n 26571 Console-Beep -h 587 -p 26571 -n 26650 Pause-Console -p 26650 -n 26765 Console-Beep -h 622 -p 26765 -n 26831 Pause-Console -p 26831 -n 26971 Console-Beep -h 523 -p 26971 -n 28078 #Change your default color scheme back in your ISE $psISE.Options.RestoreDefaults()

Notice how we don’t actually use hard coded values on the [console]::beep and Start-Sleep functions? The reason for this is that over time, the play-head for each individual console can get off just a tiny bit depending on CPU speed and the audio output buffer. These tiny delays add up over time and cause the consoles to get out of sync with each other. In order to get around this issue, we calculate the time based on its sync with the [Diagnostics.Stopwatch].

If our fixed play-head note start time isn’t activated until after the current stopwatch time, then we subtract the delay amount from the total note, allowing the notes to catch up.

Showing The PowerShell Command Window

You may be familiar with the fact that even when deploying a .ps1 in interactive mode, the PowerShell window will not be visible on the target machine. To get around this, we’re going to run an additional PowerShell process using the Start-Process command.

Start-Process powershell.exe -ArgumentList '-Command "& { Write-Output \"Hello from PDQ Deploy\" Pause }"'

Note the character escape on the quotations: \". Because we’re using the -Command "& { }" argument, all quotations need to be escaped.
On the PowerShell step, on the Options tab, set your Run As to Deploy User (Interactive)

Happy PowerShelling!

*2006 may not be that long ago, but in computer terms, we’re approaching ancient history.

company JJB
JJ Bateman

JJ is a technical creative. He finds joy in programming, automation, and in participating in the artistic sides of things. You'll often find him drinking on Thursdays on the PDQ webcast, rambling and raging remotely at the Bermuda Triangle/heisenbug-tier tech at the studio in SLC.

Related articles