1

How can I run the following command in a batch file?

POWERSHELL -Command "& {Get-AppxPackage | %% { Add-AppxPackage -ForceApplicationShutdown -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppxManifest.xml" -verbose }}"

Every time I try it I get an error:

Add-AppxPackage : A positional parameter cannot be found that accepts argument '\AppxManifest.xml'.
At line:1 char:25
+ ... ackage | % {Add-AppxPackage -ForceApplicationShutdown -DisableDevelop ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-AppxPackage], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Windows.Appx.PackageManager.Commands.AddAppxPackageCommand
Austin Reed
  • 11
  • 1
  • 5
  • The error is pretty specific. The command as written is invalid. See the PS docs for details. If you are saying it runs in a normal PS session, then that means the quoting you are using is at issue. There are lots of examples for [PS execution from a batch all over the web](https://duckduckgo.com/?q=%27run+powershell+from+a+batch+file%27&t=h_&ia=web) – postanote Jul 02 '22 at 22:12
  • It runs fine when running straight from powershell... already googled that's why I'm posting ;) I'm guessing it's with the variable, not sure how to create a variable in powershell through a batch. – Austin Reed Jul 02 '22 at 22:18
  • I've been messing in OS since the Monad days, and have never seen or ever tries the '%%' you are showing you are using. Of course '%%' in .cmd/.bat, has is use case. PS has a ton of special characters, and '%' is short for ForEach, and that '%%' would be out of place in the context where you are using it. As for variables, the error message is not indicated issues with yours. Variables are all about scope, and proper quoting in command strings. Variable need to be expanded as we know, so, you need to address that. PS provides Trace-Command and Set-Debug to check your code calls. – postanote Jul 03 '22 at 04:45
  • Take a look at these threads: How to pass batch file variables to PowerShell script https://stackoverflow.com/questions/56961935/how-to-pass-batch-file-variables-to-powershell-script Setting environment variables with batch file lauched by Powershell script https://stackoverflow.com/questions/49027851/setting-environment-variables-with-batch-file-lauched-by-powershell-script – postanote Jul 03 '22 at 04:53
  • 1
    Does this answer your question? [run powershell command from cmd](https://superuser.com/questions/1080239/run-powershell-command-from-cmd) – SimonS Jul 04 '22 at 13:56

1 Answers1

2

Well your command is still inside a batch file so the special batch characters must be escape. I always use this site it is a pretty good overview.

https://www.robvanderwoude.com/escapechars.php

From personal experience I would always choose to put the command in a file if it there is too much escaping needed.

Powershell.exe -ExecutionPolicy Bypass -File yourscript.ps1
Carsten.R
  • 46
  • 6