|
| 1 | +@echo off |
| 2 | +title Set the PowerShell Execution Policy |
| 3 | +color 0e |
| 4 | +mode 1000 |
| 5 | +cls |
| 6 | +goto Pre |
| 7 | + |
| 8 | +:Pre |
| 9 | +echo Note: If you are seeing this the second time, after already giving administrator permissions, this is normal. |
| 10 | +echo In such a case, please enter y and you will be directed to the correct place. |
| 11 | +echo ============================================================================================================= |
| 12 | +echo ============================================================================================================= |
| 13 | +echo Administrator permissions are needed to change the PowerShell Execution Level. |
| 14 | +echo Allow? |
| 15 | +set /p "choice=(Y/N)?..." |
| 16 | +if /i "%choice%" == "Y" ( |
| 17 | + goto RunAdmin |
| 18 | +) else if /i "%choice%" == "N" ( |
| 19 | + echo You have chosen to not give administrator permissions to the windows command prompt. |
| 20 | + echo As a result, you will not be able to change the PowerShell execution level. |
| 21 | + echo Command prompt will close in 15 seconds... |
| 22 | + timeout 15 |
| 23 | + exit |
| 24 | +) else ( |
| 25 | + echo Your input is not one of the choices. |
| 26 | + echo Please try again... |
| 27 | + pause |
| 28 | + cls |
| 29 | + goto Pre |
| 30 | +) |
| 31 | + |
| 32 | +:RunAdmin |
| 33 | +echo ======================================= |
| 34 | +echo Running administrator Command Prompt... |
| 35 | +echo ======================================= |
| 36 | + |
| 37 | +:init |
| 38 | +setlocal DisableDelayedExpansion |
| 39 | +set "batchPath=%~0" |
| 40 | +for %%k in (%0) do set batchName=%%~nk |
| 41 | +set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs" |
| 42 | +setlocal EnableDelayedExpansion |
| 43 | + |
| 44 | +:checkPrivileges |
| 45 | +NET FILE 1>NUL 2>NUL |
| 46 | +if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) |
| 47 | + |
| 48 | +:getPrivileges |
| 49 | +if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges) |
| 50 | +echo. |
| 51 | +echo ****************************************** |
| 52 | +echo Invoking UAC for Privilege Escalation... |
| 53 | +echo ****************************************** |
| 54 | +echo. |
| 55 | +echo Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%" |
| 56 | +echo args = "ELEV " >> "%vbsGetPrivileges%" |
| 57 | +echo For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%" |
| 58 | +echo args = args ^& strArg ^& " " >> "%vbsGetPrivileges%" |
| 59 | +echo Next >> "%vbsGetPrivileges%" |
| 60 | +echo UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%" |
| 61 | +"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %* |
| 62 | +exit /b |
| 63 | + |
| 64 | +:gotPrivileges |
| 65 | +setlocal & pushd . |
| 66 | +cd /d %~dp0 |
| 67 | +if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1) |
| 68 | +cls |
| 69 | +goto Start |
| 70 | + |
| 71 | +:Start |
| 72 | +echo Which execution level do you want to set on PowerShell? |
| 73 | +echo Choose one of the following: |
| 74 | +echo Restricted --^> RT [Will not allow any powershell scripts to run. Only individual commands may be run.] |
| 75 | +echo AllSigned --^> AS [Will allow signed powershell scripts to run.] |
| 76 | +echo RemoteSigned --^> RS (recommended^) [Allows unsigned local script and signed remote powershell scripts to run.] |
| 77 | +echo Unrestricted --^> UR [Will allow unsigned powershell scripts to run. Warns before running downloaded scripts.] |
| 78 | +echo Bypass --^> BY (not recommended^) [Nothing is blocked and there are no warnings or prompts.] |
| 79 | +set /P "choice=Your choice?..." |
| 80 | +if /I "%choice%" == "RT" ( |
| 81 | + goto RT |
| 82 | +) else if /I "%choice%" == "AS" ( |
| 83 | + goto AS |
| 84 | +) else if /I "%choice%" == "RS" ( |
| 85 | + goto RS |
| 86 | +) else if /I "%choice%" == "UR" ( |
| 87 | + goto UR |
| 88 | +) else if /I "%choice%" == "BY" ( |
| 89 | + goto BY |
| 90 | +) else ( |
| 91 | + echo "%choice%" is not a valid choice. Please type one of the given two letter shorthands for the execution level(capitalization doesn't matter^). |
| 92 | + pause |
| 93 | + cls |
| 94 | + goto Start |
| 95 | +) |
| 96 | + |
| 97 | +:RT |
| 98 | +powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Restricted -Force}" |
| 99 | +echo Success, PowerShell Execution Policy set to |
| 100 | +powershell -command "& {Get-ExecutionPolicy}" |
| 101 | +echo Command prompt will now close in 10 seconds. |
| 102 | +timeout 10 |
| 103 | +exit |
| 104 | + |
| 105 | +:AS |
| 106 | +powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy AllSigned -Force}" |
| 107 | +echo Success, PowerShell Execution Policy set to |
| 108 | +powershell -command "& {Get-ExecutionPolicy}" |
| 109 | +echo Command prompt will now close in 10 seconds. |
| 110 | +timeout 10 |
| 111 | +exit |
| 112 | + |
| 113 | +:RS |
| 114 | +powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force}" |
| 115 | +echo Success, PowerShell Execution Policy set to |
| 116 | +powershell -command "& {Get-ExecutionPolicy}" |
| 117 | +echo Command prompt will now close in 10 seconds. |
| 118 | +timeout 10 |
| 119 | +exit |
| 120 | + |
| 121 | +:UR |
| 122 | +powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}" |
| 123 | +echo Success, PowerShell Execution Policy set to |
| 124 | +powershell -command "& {Get-ExecutionPolicy}" |
| 125 | +echo Command prompt will now close in 10 seconds. |
| 126 | +timeout 10 |
| 127 | +exit |
| 128 | + |
| 129 | +:BY |
| 130 | +powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Bypass -Force}" |
| 131 | +echo Success, PowerShell Execution Policy set to |
| 132 | +powershell -command "& {Get-ExecutionPolicy}" |
| 133 | +echo Command prompt will now close in 10 seconds. |
| 134 | +timeout 10 |
| 135 | +exit |
0 commit comments