|
1 | 1 | @echo off |
2 | 2 | title Set the PowerShell Execution Policy |
3 | | -color 0a |
| 3 | +color 0c |
4 | 4 | mode 1000 |
5 | 5 | cls |
6 | | -goto Precheck |
| 6 | +goto Pre |
7 | 7 |
|
8 | | -:Precheck |
9 | | -echo Just a reminder that this script needs to be run as administrator. |
10 | | -echo If this script already is, then proceed on as normal. |
11 | | -echo If not, please close the window and reopen with administrator permissions. |
12 | | -pause |
| 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 |
13 | 69 | goto Start |
14 | 70 |
|
15 | 71 | :Start |
16 | 72 | echo Which execution level do you want to set on PowerShell? |
17 | 73 | echo Choose one of the following: |
18 | | -echo Restricted = 'RT' |
19 | | -echo AllSigned = 'AS' |
20 | | -echo RemoteSigned = 'RS' (recommended) |
21 | | -echo Unrestricted = 'UR' |
22 | | -echo Bypass = 'BY' (not recommended) |
23 | | -set choice= |
24 | | -set /p choice=You choice?: |
25 | | -if NOT '%choice%'=='' set choice=%choice:~0,1% |
26 | | -if '%choice%'=='RT' goto RT |
27 | | -if '%choice%'=='rt' goto RT |
28 | | -if '%choice%'=='AS' goto AS |
29 | | -if '%choice%'=='as' goto AS |
30 | | -if '%choice%'=='RS' goto RS |
31 | | -if '%choice%'=='rs' goto RS |
32 | | -if '%choice%'=='UR' goto UR |
33 | | -if '%choice%'=='ur' goto UR |
34 | | -if '%choice%'=='BY' goto BY |
35 | | -if '%choice%'=='by' goto BY |
36 | | -echo "%choice%" is not a valid choice. Please type one of the given two letter shorthands for the execution level(capitalization doesn't matter). |
37 | | -pause |
38 | | -cls |
39 | | -goto Start |
| 74 | +echo Restricted = RT |
| 75 | +echo AllSigned = AS |
| 76 | +echo RemoteSigned = RS (recommended) |
| 77 | +echo Unrestricted = UR |
| 78 | +echo Bypass = BY (not recommended) |
| 79 | +set /p "choice=Your choice?..." |
| 80 | +if /i "%choice%" == "RT" ( |
| 81 | + goto RT |
| 82 | +) |
| 83 | +else if /i "%choice%" == "AS" ( |
| 84 | + goto AS |
| 85 | +) |
| 86 | +else if /i "%choice%" == "RS" ( |
| 87 | + goto RS |
| 88 | +) |
| 89 | +else if /i "%choice%" == "UR" ( |
| 90 | + goto UR |
| 91 | +) |
| 92 | +else if /i "%choice%" == "BY" ( |
| 93 | + goto BY |
| 94 | +) |
| 95 | +else ( |
| 96 | + echo "%choice%" is not a valid choice. Please type one of the given two letter shorthands for the execution level(capitalization doesn't matter). |
| 97 | + pause |
| 98 | + cls |
| 99 | + goto Start |
| 100 | +) |
40 | 101 |
|
41 | 102 | :RT |
42 | 103 | powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Restricted -Force}" |
|
0 commit comments