Run ps1 with admin privileges and unrestricted policies from a batch file?

8,577

You have multiple problems:

  1. You have nested double-quotes that are confusing it, you need to escape inner quotes, and/or use single quotes inside the double quotes.
  2. You have extra spaces in your call to "%~DPN0".
  3. You are incorrectly concatenating the script name variable (%~DPN0) into your ArgumentList.
  4. 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.
  5. 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.
  6. 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 the Set-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"
Share:
8,577

Related videos on Youtube

BrianC
Author by

BrianC

I'm student of high school

Updated on September 18, 2022

Comments

  • BrianC
    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

  • Ƭᴇcʜιᴇ007
    Ƭᴇcʜιᴇ007 almost 8 years
    Works 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
    Ƭᴇcʜιᴇ007 almost 8 years
    Also, for testing, make a test.ps1 that simply contains a basic PowerShell script (like write-host "Hi!" followed by read-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
    BrianC almost 8 years
    Yes, 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
    Ƭᴇcʜιᴇ007 almost 8 years
    If you have an different/additional problem, please start a new question instead of changing this one into something else...
  • BrianC
    BrianC almost 8 years
    no. all works fine. sorry