etc
etc

Reputation: 115

Calling same-directory audio file in PowerShell

I want to create a PowerShell script that sets the system volume to a specified level, and then runs an audio file found in the same directory as the script. I've figured out the first part, but I can't seem to manage the second. It is important that the folder can be moved around and renamed, and the script should still work. This creates the problem that I cannot simply use Invoke-Item and then specify the filename, as the path is subject to change.

Edit: My attempt:

$player = New-Object System.Media.SoundPlayer "$env:userprofile\SoundFile.wav"
$player.Play()

Start-Sleep -s 10

This has the problem that it doesn't work if the path is changed.

Upvotes: 0

Views: 233

Answers (1)

etc
etc

Reputation: 115

Found a solution: I used the automatically defined variable $PSScriptRoot, which had apparently been added in PS 3.0. So the line is now

$player = New-Object System.Media.SoundPlayer "$PSScriptRoot\SoundFile.wav"

Upvotes: 1

Related Questions