0

I have a file called f.wav in my temp folder. I am trying to use powershell's System.Media.SoundPlayer to play the wave file from within powershell.

$PlayWav=New-Object System.Media.SoundPlayer;$PlayWav.SoundLocation='C:\Users\USER\AppData\Local\Temp\f.wav';$PlayWav.playsync()

The above snippet works when I hardcode the files path location without a problem, however when I use the enviroment variable like in the snippet below this it throws an error.

$PlayWav=New-Object System.Media.SoundPlayer;$PlayWav.SoundLocation='$env:TMP\f.wav';$PlayWav.playsync()

The error that is thrown up is: Exception setting "SoundLocation": "The given path's format is not supported."

Is it just not possible using the temp folders enviroment variable?

I am Jakoby
  • 577
  • 4
  • 19
  • 3
    single-quotes don't allow the variable `$env:TMP` to expand. Use double-quotes instead. – Santiago Squarzon Mar 11 '22 at 00:50
  • 2
    In short: Only `"..."` strings (double-quoted, called _expandable strings_) perform string interpolation (expansion of variable values) in PowerShell, not `'...'` strings (single-quoted, called _verbatim strings_): see [this answer](https://stackoverflow.com/a/40445998/45375) for an overview of PowerShell's _expandable strings_ and [this answer](https://stackoverflow.com/a/55614306/45375) for an overview of PowerShell _string literals in general_. – mklement0 Mar 11 '22 at 02:20

0 Answers0