Reputation: 1422
Ok, I've written a script that works flawlessly in Windows 8. I switch over to Windows 7 and get all kinds of blood. After some debugging, I found the problem. Win 8 uses PowerShell 3.0 and Win 7 uses PowerShell 2.0 AND PowerShell 3.0 allows you to use double quotes when using:
$ExecutionContext.InvokeCommand.ExpandString($var)
Since my code heavily relies on this with many of the strings using double quotes, I'm trying to find a solution.
Here-Strings have not worked because some of my strings are as such:
$var = 'this is a "sample" of $one of my strings'
The here-string workaround only works if I output to a text file, which I do not want since I heavily use this code in a function.
I found on Microsoft's website a bug related to this. The way it works in PowerShell 3.0 is how it was originally intended to work. Also, upgrading Windows 7 to PowerShell 3.0 is out of the question.
Upvotes: 2
Views: 1688
Reputation: 201672
You can test for PowerShell versions less than 3 and then escape any double quotes like so:
C:\PS> if ($PSVersionTable.PSVersion.Major -lt 3) { $var = $var -replace '"','`"' }
C:\PS> $ExecutionContext.InvokeCommand.ExpandString($var)
this is a "sample" of one baby of my strings
Upvotes: 6