Skip to content

Instantly share code, notes, and snippets.

@rufflabs
Created May 23, 2015 14:34
Show Gist options
  • Save rufflabs/908a24a73a651f01b934 to your computer and use it in GitHub Desktop.
Save rufflabs/908a24a73a651f01b934 to your computer and use it in GitHub Desktop.
Returns true or false depending on if you are an administrator
function Test-Administrator
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
@rufflabs
Copy link
Author

Elevated relaunch:

if(-not Test-Administrator) {
    Start-Process powershell -ArgumentList $PSCommandPath -Verb RunAs
}

Also, could add #requires -RunAsAdministrator at the top to prevent launching without admin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment