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.
Nice one