Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run PowerShell as an administrator AND with arguments from a batch file?

I've found answers for both tasks separately, but not together:

  • Run PowerShell as an administrator from a batch file

  • Run PowerShell with arguments from a batch file

But I want to run PowerShell as an administrator AND with arguments from a batch file.

For this reason, I'm convinced this isn't a duplicate so far.

This problem is complicated for this reason:

I'm using the ArgumentList switch to point to the file path when running an elevated PowerShell. I'm also using it to pass in arguments. Separately, I have been able to use this switch with success. It doesn't seem as though I can use it for both tasks though.

Here's how I'm using PowerShell currently:

As an administrator:

powershell -Command "& {Start-Process powershell -ArgumentList '-File "filePath.ps1' -Verb RunAs}"

With arguments:

powershell -ExecutionPolicy unrestricted Import-Module "filePath.ps1" -ArgumentList "arg1","arg2"

What I've tried:

I've tried just adding my arguments to the ArgumentList where the file is passed in, but they weren't recognized.

I also tried adding -Verb RunAs to the working argument line, but PowerShell thought that I was trying to pass in another argument called Verb, and failed to run PowerShell as and admin.

Is it possible to accomplish both running PowerShell as an administrator, and with arguments, by switching up how I'm passing in arguments, or how I'm elevating the PowerShell?

like image 720
Kyle Stoflet Avatar asked Nov 03 '16 20:11

Kyle Stoflet


People also ask

How do I run a PowerShell script with Administrator privileges?

Use PowerShell in Administrative Mode If you need to run a PowerShell script as an administrator, you will need to open PowerShell in administrative mode. To do so, find PowerShell on the Start menu, right click on the PowerShell icon, and then select More | Run as Administrator from the shortcut menu.

How do I run a batch file as Administrator in PowerShell?

When first creating shortcuts, there is a requirement to right-click on the shortcut, select “Properties”, go to the Shortcut tab and then click “Advanced…” to choose the “Run as Administrator” option. Shortcuts to the *. bat files used to execute PowerShell scripts should be set to “Run as Administrator” by default.

What are the different ways you can run PowerShell in Administrator mode?

With Administrative privileges (Run as administrator)Click Start, type PowerShell, right-click Windows PowerShell, and then click Run as administrator.

How do I run a PowerShell script from the command line with arguments?

To pass command line arguments to a PowerShell ps1 file, run PowerShell.exe with the -File command line argument. Then, specify the ps1 file followed by its arguments (if any).


2 Answers

It is possible to solve your problem without third-party tools, but the solution is somewhat arcane.

Therefore, consider downloading Bill Stewart's helpful elevate32 and elevate64 tools instead, as described in his answer, which simplifies the solution.

Here's a simple example that invokes the Get-Date cmdlet with arguments in an elevated PowerShell session launched from cmd.exe (or a batch file):

powershell -command "Start-Process -verb runas powershell" "'-noexit -command get-date -UFormat %s'"

Note how the command line to pass through to the elevated PowerShell session that the intermediary -command argument creates is passed as a single argument enclosed in embedded single quotes.

Quoting may get tricky, but this approach also works with invoking *.ps1 files in principle:

  • powershell -command "Start-Process -verb runas powershell" is the invariant part of the command line.

  • The remaining "..."-enclosed string must contain a nested single string with quoting that PowerShell recognizes (single quotes are easiest) containing all arguments that you would pass directly to a powershell ... command-line invocation.

Applied to your example:

... "'-File C:\path\to\filePath.ps1 arg1 arg2'"

Note: Be sure to use the full path to your script file, because the elevated PowerShell session does not (necessarily) run in the same directory as the calling session.

If you need to quote the arguments inside the nested string, use \":

... "'-File \"c:\path with spaces\to\filePath.ps1\" arg1 arg2'"
like image 66
mklement0 Avatar answered Oct 02 '22 13:10

mklement0


You can do this by grabbing a short command-line executable I wrote called Elevate32.exe (or Elevate64.exe for the 64-bit version). You can get it here:

http://www.westmesatech.com/misctools.html (ElevationToolkit1.zip)

Example:

elevate64 -- powershell.exe -file C:\scripts\myscriptfile.ps1

Everything after -- is the command line you want to elevate.

like image 44
Bill_Stewart Avatar answered Oct 02 '22 12:10

Bill_Stewart