powershell command to reinstall single Windows 10 app
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.
21 Answer
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 JonesNotice 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"" 2