Created
September 9, 2022 14:01
-
-
Save chaojian-zhang/5de7491c8e1bf8ae0766f39edde674cc to your computer and use it in GitHub Desktop.
Self-elevate script access priviledge.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Self-elevate the script - put this at beginning of file | |
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process pwsh.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; Exit } | |
# Example of a command that requires admin priviledge | |
[Environment]::SetEnvironmentVariable("MY_ENV_PATH", "$PSScriptRoot", "Machine") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment