I use several one line powershell commands in our servers batch file login script but I can't figure out what I'm doing wrong with this one.
powershell.exe -ExecutionPolicy Bypass -Command "Get-AppxPackage -allusers *Windows.Photos* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register '$($_.InstallLocation)\AppXManifest.xml'}"
When I try to run this I get the error: Cannot find path 'C:\$($_.InstallLocation)\AppXManifest.xml'
I am guessing there is a problem with the quoting in the command but I have tried different ways and can't get it to work. If I run the command below from a powershell prompt it works fine.
Get-AppxPackage -allusers *Windows.Photos* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}
I would like to avoid having to create a separate .ps1 file and keep it in a one liner if possible.
-
1Your ending single quote and double quote syntax is incorrect in the first command.Ramhound– Ramhound2016年09月21日 15:25:07 +00:00Commented Sep 21, 2016 at 15:25
-
Could you please elaborate? I figured I was doing something wrong with the quotes but no matter what combinations of double or single quotes I try I can't get it to work.D. Stevenson– D. Stevenson2016年09月21日 16:04:01 +00:00Commented Sep 21, 2016 at 16:04
1 Answer 1
In powershell, strings in single quotes (literal strings) are treated slightly differently to those in double quotes (interpolated strings).
To see this, consider the following
$name = "Jones"
'Hello $name'
"Hello $name"
This will output:
Hello $name
Hello Jones
Notice how the variable was not expanded in the single quoted (literal) string, but was expanded in the double quoted (interpolated string)
Back to your issue, the problem is the Register argument on Add-AppxPackage has single quotes around what should be an interpolated string. To escape the double quotes in a batch file, you'll need to use two consecutive double quotes (i.e. ""). In other words, replace
-Register '$($_.InstallLocation)\AppXManifest.xml'
with
-Register ""$($_.InstallLocation)\AppXManifest.xml""
-
Alex, thanks for your reply. When I try the command with double quotes as suggested I now receive the error: The string is missing the terminator: ".D. Stevenson– D. Stevenson2016年09月26日 20:29:16 +00:00Commented Sep 26, 2016 at 20:29
-
The complete command I am trying now is:
powershell.exe -ExecutionPolicy Bypass -Command "Get-AppxPackage -AllUsers *Windows.Photos* | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register ""$($_.InstallLocation)\AppXManifest.xml""}"D. Stevenson– D. Stevenson2016年09月26日 20:36:41 +00:00Commented Sep 26, 2016 at 20:36