Run ps1 with admin privileges and unrestricted policies from a batch file?
8,577
You have multiple problems:
- You have nested double-quotes that are confusing it, you need to escape inner quotes, and/or use single quotes inside the double quotes.
- You have extra spaces in your call to "%~DPN0".
- You are incorrectly concatenating the script name variable (%~DPN0) into your ArgumentList.
- Start-Processes
ArgumentList
takes an array of strings (string[]
), not a space-separated list. To pass a static array of strings, separate the string items with commas. - You are also trying to run a
-command
and a-file
, and you can't use both a-file
argument and a-command
argument in a single PowerShell.exe call. - You are trying to run a PowerShell command (
Set-ExecutionPolicy
) without specifying the-command
argument. To set the execution policy for the Powershell.exe session you are trying to run, use PowerShell.exe's-ExecutionPolicy
parameter instead of trying to run theSet-ExecutionPolicy
command.
Here's is the corrected version of your command:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell -ArgumentList '-ExecutionPolicy Unrestricted','-File %~dpn0.ps1' -Verb RunAs"
Related videos on Youtube
Comments
-
BrianC over 1 year
I have a test.bat to run a test.ps1 with admin privileges and unrestricted policies, and both are in the same folder (local PC), but the bat does't run ps1. Whats wrong?
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy Unrestricted -Force -File" "% ~ dpn0.ps1" "' -Verb RunAs}"
And my script is similar to this
-
DavidPostill almost 8 yearsWhat is the error message?
-
DavidPostill almost 8 years
-
-
Ƭᴇcʜιᴇ007 almost 8 yearsWorks fine here, and we're not a script writing service, so you'll have to do a little more, and provide more information. Please edit your question and add exactly what you've attempted, and exactly what the results were of those tries, including the exact output from the batch file when you attempt to run it.
-
Ƭᴇcʜιᴇ007 almost 8 yearsAlso, for testing, make a test.ps1 that simply contains a basic PowerShell script (like
write-host "Hi!"
followed byread-host
so it waits for you to hit enter), this will help determine if it's launching incorrectly, or if it's a problem with the PS1 script you're trying to run. -
BrianC almost 8 yearsYes, you are right. The problem is ps1. I fix it, but a have another problem, an re-edit my question to helpme with the second part, to select your answer with correct. Thanks a lot
-
Ƭᴇcʜιᴇ007 almost 8 yearsIf you have an different/additional problem, please start a new question instead of changing this one into something else...
-
BrianC almost 8 yearsno. all works fine. sorry