Poweshell script not starting, while using arg -ExecutionPolicy Bypass


  1. Posts : 582
    Windows 10 Pro 64 bit 19044.1706
       #1

    Poweshell script not starting, while using arg -ExecutionPolicy Bypass


    I want to launch specific ps1 script using -ExecutionPolicy Bypass, because it doesn't work otherwise.

    I found this line from multiple sources, but when i execute this command in a bat file, or directly in PowerShell. It just hangs and nothing happens...
    Code:
    powershell.exe -ExecutionPolicy Bypass -File C:\MyUnsignedScript.ps1
    I launched powershell as admin. I have set restricted policy on machine. But i read, this command should bypass it. Yet it doesn't work. I tried putting -ExecutionPolicy at the end, or using quotes for path. Nothing works...
      My Computer


  2. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @empleat,

    empleat said:
    I want to launch specific ps1 script using -ExecutionPolicy Bypass, because it doesn't work otherwise.

    I found this line from multiple sources, but when i execute this command in a bat file, or directly in PowerShell. It just hangs and nothing happens...
    Code:
    powershell.exe -ExecutionPolicy Bypass -File C:\MyUnsignedScript.ps1
    I launched powershell as admin. I have set restricted policy on machine. But i read, this command should bypass it. Yet it doesn't work. I tried putting -ExecutionPolicy at the end, or using quotes for path. Nothing works...

    You could try this first . . .
    Code:
    PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\MyUnsignedScript.ps1""' -Verb RunAs}"

    These BOTH work in .bat files to automatically give Administrator Rights . . .
    Code:
    (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
    Code:
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /b )
    Just put one of them at the TOP of the batch script and CALL your file.

    I hope this helps!
    Last edited by Paul Black; 22 Aug 2020 at 17:22.
      My Computer


  3. Posts : 582
    Windows 10 Pro 64 bit 19044.1706
    Thread Starter
       #3

    Paul Black said:
    Hello @empleat,



    You could try this first . . .
    Code:
    PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\MyUnsignedScript.ps1""' -Verb RunAs}"

    These BOTH work in .bat files to automatically give Administrator Rights . . .
    Code:
    (Net session >nul 2>&1)||(PowerShell start """%~0""" -verb RunAs & Exit /B)
    Code:
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /b )
    Just put one of them at the TOP of the batch script and CALL your file.

    I hope this helps!
    Thanks!!! Great first line worked!!!
      My Computer


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #4

    Hello @empleat,

    empleat said:
    Thanks!!! Great first line worked!!!
    You are VERY welcome!
      My Computer


  5. Posts : 7,607
    Windows 10 Home 20H2
       #5

    The third code in post #2 can be simplified as below:

    Code:
    (Net session >nul 2>&1)&&Goto:✱||Echo CreateObject^("Shell.Application"^). ^
    ShellExecute "%~0","✶","","RunAs",1 >"%temp%\(+).vbs"&&"%temp%\(+).vbs"&Exit
    :✱
      My Computer


  6. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #6

    Hello @Matthew Wai,

    Matthew Wai said:
    The third code in post #2 can be simplified as below:
    Code:
    (Net session >nul 2>&1)&&Goto:✱||Echo CreateObject^("Shell.Application"^). ^
    ShellExecute "%~0","✶","","RunAs",1 >"%temp%\(+).vbs"&&"%temp%\(+).vbs"&Exit
    :✱
    Interesting concept.

    Perhaps you could explain the use of variable please!

    Thanks
      My Computer


  7. Posts : 7,607
    Windows 10 Home 20H2
       #7

    The code is not just a concept. It actually works and has been used in the BSOD batch scripts:
    Batch files for use in BSOD debugging

    The star label is used because it is nice-looking.
      My Computer


  8. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #8

    Matthew Wai said:
    It actually works and has been used in the BSOD batch scripts:

    Batch files for use in BSOD debugging

    The star label is used because it is nice-looking.
    Thanks @Matthew.

    The interesting thing is that the star label shows as a small box on the Tenforums screen, and a question mark on my OS!
    Last edited by Paul Black; 24 Aug 2020 at 08:18.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 09:10.
Find Us




Windows 10 Forums