다음을 통해 공유


PowerShell Tricks: Play Song

This trick comes way back from the  PowerShell Bangalore User Group.

At the first meet, someone asked on how you use PowerShell outside work.

Answer: Too lazy to select a song at random and play them so we used below PowerShell one-liner to do that.

001
Get-ChildItem -Path D:\Songs -Filter *.mp3 -Recurse | Get-Random -Count 1 | Invoke-Item

 

If in the mood to create a playlist of random songs then again PowerShell comes to the rescue.

Before we start creating a playlist of random songs, we have to set VLC Player as the default association for .MP3 files.

We can do that using good old assoc & ftype executable in PowerShell, using below code (provided you have VLC Player installed). Note that setting the file-type associations is a one-time activity.

001
002
003
004

$null = cmd /c 'assoc .mp3=VLC.mp3'
$null = cmd /c 'ftype VLC.mp3="C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" --started-from-file "%1"'

Now time for the one-liner to create a random playlist of songs:

001
002
Get-ChildItem D:\Songs -recurse -filter *.mp3| Get-Random -Count 8 |
        ForEach-Object -process { Start-Process $_.Fullname -verb 'AddtoPlaylistVLC'}

If you wish to explore more on this topic here's two blog posts on them:

PowerShell to Create a random Music Playlist in VLC

PowerShell : Play Music until Sleep