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?