Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Music of the Shell

Posted on August 27, 2010March 28, 2011

We made it to the end of the week, and I don't know about you but I have my head buried in PowerShell work. But you know what they say about all work and no fun...so I figured I'd take a break from serious PowerShell and do something a little fun. Although, I suppose you might pick up a pointer or too along the way. This installment of Friday Fun, actually the first one, is about making beautiful music with Powershell.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

I trust you're familiar with the `a escape sequence. If not, try this.
[cc lang="powershell"]
PS C:\> write `a
[/cc]
What PowerShell is really doing is calling the Beep method of the [Console] .NET class. But there's a bit more that can be done. You can specify an frequency and a duration, in milliseconds.
[cc lang="powershell"]
PS C:\> [console]::beep(440,500)
[/cc]

In fact, given the right frequencies you can play "music" in PowerShell.
[cc lang="PowerShell"]
$scale=@{
MidC=261.6
CSharp=277.2
D=293.7
DSharp=311.1
E=329.6
F=349.2
FSharp=370.0
G=392.0
GSharp=415.3
A=440.0
ASharp=466.2
B=493.9
C=523.2
}

#play a chromatic scale
write-host "First octave" -ForegroundColor Cyan
$scale.values | sort | foreach {[console]::beep($_,125)}
[/cc]
Or, you can be more avant-garde and listen to the music of chaos. I put together a script that plays a set number of notes with a random frequency and of random duration.
[cc lang="Powershell"]
#requires -version 2.0

#usage: music.ps1 64

Param([int]$Length=16)

#function to create the musical tones
Function Make-Music {

Param(
[Parameter(Position=0, ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[int]$frequency=400,
[Parameter(Position=1, ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[int]$duration=500)

Process {
[console]::Beep($frequency,$duration)
}
} #function

Write-Host "Generating a $length note sequence" -ForegroundColor Cyan

$notes=for ($i=1;$i -le $length;$i++) {
$f=Get-Random -Minimum 500 -Maximum 2500
$r=Get-Random -Minimum 1 -Maximum 10
New-Object -TypeName PSObject -Property @{
Frequency=$f
Duration=$r*125
}
} #for

$notes | make-music

$a=Read-Host "Do you want to save this to an XML file? [YN]"
if ($a -match "^Y$") {
$file=Read-Host "Enter a filename or press Enter to cancel."
if ($file) {
$notes | Export-Clixml -Path $file
}
}
[/cc]
The script takes an integer for the length of the "musical" sequence. I used a For construct to create a series of custom objects with a frequency and duration.
[cc lang="powershell"]
$notes=for ($i=1;$i -le $length;$i++) {
$f=Get-Random -Minimum 500 -Maximum 2500
$r=Get-Random -Minimum 1 -Maximum 10
New-Object -TypeName PSObject -Property @{
Frequency=$f
Duration=$r*125
}
} #for
[/cc]
This collection of objects is then piped to a nested function that can accept pipelined input which generates the sequence. If you like what you hear you can also export the sequence to an XML file. You'll be prompted for a file name. With the XML "score" you can play your "music" at any time, assuming you've loaded the Make-Music function into your shell.
[cc lang="PowerShell"]
PS C:\> import-clixml c:\work\opus1.xml | Make-Music
[/cc]
You can download a zip file with everything you need to get started here.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: Music of the Shell”

  1. Jeffery Hicks says:
    August 27, 2010 at 11:12 am

    The Sound of Files

    dir C:\Somewhere | where {$_.length -gt 37 -AND $_.length -lt 32767} | foreach {make-music $_.length 250}

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d