0

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.

  • 1
    Your ending single quote and double quote syntax is incorrect in the first command. – Ramhound Sep 21 '16 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 Sep 21 '16 at 16:04
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 Sep 26 '16 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 Sep 26 '16 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.