Skip to content

Instantly share code, notes, and snippets.

@chaojian-zhang
Created September 9, 2022 14:01
Show Gist options
  • Save chaojian-zhang/5de7491c8e1bf8ae0766f39edde674cc to your computer and use it in GitHub Desktop.
Save chaojian-zhang/5de7491c8e1bf8ae0766f39edde674cc to your computer and use it in GitHub Desktop.
Self-elevate script access priviledge.
# 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