Home > PowerShell > PowerShell: Get Current User’s Security Permissions

PowerShell: Get Current User’s Security Permissions

I was writing a little script and thought that needs to ensure it has sufficient privileges. I thought it would be more useful to find if the current user is part of any of the built in user roles. Here is my effort:

function
Get-CurrentUserRoles {

    $SecurityPrinciple
=
New-Object
-TypeName
System.Security.Principal.WindowsPrincipal
-ArgumentList ([System.Security.Principal.WindowsIdentity]::GetCurrent())

    $RolesHash
= @{}

    [System.Enum]::GetNames(“System.Security.Principal.WindowsBuiltInRole”) | ForEach-Object {

        $RolesHash[$_] =
$SecurityPrinciple.IsInRole([System.Security.Principal.WindowsBuiltInRole]::$_)

    }

    $RolesHash

}

 

PS C:\> Get-CurrentUserRoles

 

Name Value

—- —–

Administrator True

User True

Guest False

PowerUser False

AccountOperator False

SystemOperator False

PrintOperator False

BackupOperator False

Replicator False

 

 

PS C:\> (Get-CurrentUserRoles).Administrator

True

 

PS C:\> (Get-CurrentUserRoles).Guest

False

 

 

As you can see, you get back PowerUser, Administrator, SystemOperator, etc.

Advertisements
Categories: PowerShell
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: