1

So I'm trying to create a batch file to run a PowerShell script while bypassing the execution policy. Oddly, it worked a single time, but without me editing anything, it will not run again. I've created other files thinking maybe my file somehow got corrupted, but nothing... Any chance someone sees anything blatantly wrong with this?

@echo off
Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0File.ps1' -Verb RunAs}"
PAUSE

The *.ps1 file works by itself if I click through the prompts. Also, if I manually set the execution policy in PowerShell to Bypass, this batch file still does not work. This is not a process I usually need to take, so I'm curious if anyone sees anything wrong with how this is written?

8
  • Is it getting stuck on the pause without user input? Commented Mar 11, 2021 at 19:16
  • Your example seems to work for me. Commented Mar 11, 2021 at 19:26
  • I'm not seeing the pause at all. I see a CMD window pop up for a moment, a PowerShell window pop up for a moment, and then they both vanish with seemingly nothing happening. But the pause is after the PowerShell line, so shouldn't it pause after that runs? Commented Mar 11, 2021 at 19:26
  • 3
    Wow ... that's new. ;-) ... you're using one command line interface to start another command line interface to start the second command line interface again. ;-) You could simply use a link with the command line you need. Like Powershell -ep bypass -file 'Your Powershell script' ... and you cannot bypass the Execution Policy for scripts from inside a script. ;-) Commented Mar 11, 2021 at 19:28
  • 1
    @ChristopherCass ... wow, that's even worse. It sounds like the tail's wagging the dog. ;-) No offense ... I'd recommend that you try to get some help from a trained person. I'm pretty sure there must be a cleaner way to do whatever you do. Commented Mar 11, 2021 at 21:25

1 Answer 1

2

If this is just to run your script, what I personally do is create a shortcut of the script and then modify the Target of the shortcut:

Target: Powershell.exe -ExecutionPolicy Bypass -File "C:\scriptpath\script.ps1"

If you want your script to be executed as Administrator you can add this to the top of the main script:

$myInvoke="-file `"$($MyInvocation.ScriptName)`""
Start-Process "$PSHome\powershell.exe" -Verb Runas -ArgumentList $myInvoke -EA 'Stop'

If the shortcut will always be in the same folder as your script you can also leave Start In blank and change the path for Powershell.exe -ExecutionPolicy Bypass -File ".\script.ps1" by doing so if you copy the entire folder to a different location, the shortcut will still work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.