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.

asked Sep 21, 2016 at 15:13
2
  • 1
    Your ending single quote and double quote syntax is incorrect in the first command. Commented 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. Commented Sep 21, 2016 at 16:04

1 Answer 1

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""
answered Sep 25, 2016 at 15:37
2
  • 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: ". Commented 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""}" Commented Sep 26, 2016 at 20:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.