2

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
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
    – postanote
    Commented Jul 2, 2022 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. Commented Jul 2, 2022 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
    Commented Jul 3, 2022 at 4:45
  • Take a look at these threads: How to pass batch file variables to PowerShell script stackoverflow.com/questions/56961935/… Setting environment variables with batch file lauched by Powershell script stackoverflow.com/questions/49027851/…
    – postanote
    Commented Jul 3, 2022 at 4:53
  • 1
    Does this answer your question? run powershell command from cmd
    – SimonS
    Commented Jul 4, 2022 at 13:56

1 Answer 1

3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .