checking user privilages of account running script

   I have a powershell script that occasionally fails on systems that aren't running it with the correct account.

What I want to do is put a check at the beginning of the script to check that the user running the script has admin rights.

In Linux bash scripts I'd use something like this:

if [ "$(id -u)" != "0" ]; then

 I've run a few internet searches but I've not been able to find anything...
What is the powershell equivelant to this?

any help would be much appreciated

January 10th, 2014 6:06pm

Hi The Bellsprout,

In addition, the function below may be helpful for you to check whether the user running the script has the admin right

function Test-IsAdmin {
    try {
        $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
        $principal = New-Object Security.Principal.WindowsPrincipal -ArgumentList $identity
        return $principal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator )
    } catch {
        throw "Failed to determine if the current user has elevated privileges. The error was: '{0}'." -f $_
    }
}

I hope this helps.
January 12th, 2014 10:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics