(2017-06-08) WHOAMI, The PowerShell Way
Posted by Jorge on 2017-06-08
Many of you probably know the command:
WHOAMI /USER /GROUPS
Figure 1: Executing WHOAMI
–
Every wanted to do the same in PowerShell?
[Security.Principal.WindowsIdentity]::GetCurrent())
Figure 2: Executing WHOAMI The PowerShell Equivalent
–
Although it gives you the required information, it still does not list the groups in a nice way. So let’s try that!
Unfortunately not a on-liner, but rather 2 lines of code
–
$tableLayout = @{Expression={((New-Object System.Security.Principal.SecurityIdentifier($_.Value)).Translate([System.Security.Principal.NTAccount])).Value};Label="Group Name";Width=40},@{Expression={$_.Value};Label="Group SID";Width=45},@{Expression={$_.Type};Label="Group Type";Width=75}
([Security.Principal.WindowsIdentity]::GetCurrent()).Claims | FT $tableLayout
Figure 3: List Of Group Names, Group SIDs And Group Types From The Access Token
–
–
Cheers,
Jorge
————————————————————————————————————————————————————-
This posting is provided "AS IS" with no warranties and confers no rights!
Always evaluate/test everything yourself first before using/implementing this in production!
This is today’s opinion/technology, it might be different tomorrow and will definitely be different in 10 years!
DISCLAIMER: https://jorgequestforknowledge.wordpress.com/disclaimer/
————————————————————————————————————————————————————-
########################### Jorge’s Quest For Knowledge ##########################
#################### http://JorgeQuestForKnowledge.wordpress.com/ ###################
————————————————————————————————————————————————————-
Matt Corr said
Nice script!
One suggestion: remove the width attribute and then put the -AutoSize param on the Format-Table.
Example:
$tableLayout = @{Expression={((New-Object System.Security.Principal.SecurityIdentifier($_.Value)).Translate([System.Security.Principal.NTAccount])).Value};Label=”Group Name”},
@{Expression={$_.Value};Label=”Group SID”},
@{Expression={$_.Type};Label=”Group Type”}
([Security.Principal.WindowsIdentity]::GetCurrent()).Claims | Format-Table $tableLayout -AutoSize
This will let PowerShell determine the width for you to ensure that all the text is displayed.
LikeLike
Powershell fun – Zewwy's Info Tech Talks said
[…] there’s got to be an easy way to do the same cmd as whoami /groups in powershell right… wtf is going on here?!?! I thought PowerShell was the easier scripting language… man this is […]
LikeLike
Ken Young said
I need to use this to run on a users computer to check for a particular group. Any ideas? I’m new to powershell.
LikeLike