Get-CurrentUser.ps1


<#PSScriptInfo
 
.VERSION 1.0.2
 
.GUID 1a23fc08-2fdb-4d18-a1b2-53e531a4530c
 
.AUTHOR Saw-Friendship
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
Username SID MemberOf CurrentUser Security.Principal.WindowsIdentity
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Get Username, SID and MemberOf for CurrentUser from [Security.Principal.WindowsIdentity]
 
.EXAMPLE
Get-CurrentUser
 
Name : Domain\saw-friendship
SID : S-1-5-21-2825421524-4144269381-457877587-20115
AuthenticationType : Kerberos
ImpersonationLevel : None
IsAuthenticated : True
IsGuest : False
IsSystem : False
IsAnonymous : False
 
 
.EXAMPLE
Get-CurrentUser -IncludeMemberOf
 
Name : Domain\saw-friendship
SID : S-1-5-21-2825421524-4144269381-457877587-20115
AuthenticationType : Kerberos
ImpersonationLevel : None
IsAuthenticated : True
IsGuest : False
IsSystem : False
IsAnonymous : False
MemberOf : {@{Name=Domain\Domain Users; SID=S-1-5-21-2825421524-4144269381-457877587-513}, @{Name=���; SID=S-1-1-0}, @{Name=PC1\docker-users; SID=S-1-5-21-2113150-1802504319-1219586930-1006}, @{Name=PC1\Ssh Users; SID=S-1-5-21-2113150-1802504319-1219586930-1005}...}
 
 
 
#>
 

param (
    [switch]$IncludeMemberOf
)

$SelectProperty = @(
    @{'Name' = 'Name'; 'Expression' = {$_.Name}},
    @{'Name' = 'SID'; 'Expression' = {$_.User}},
    @{'Name' = 'AuthenticationType'; 'Expression' = {$_.AuthenticationType}},
    @{'Name' = 'ImpersonationLevel'; 'Expression' = {$_.ImpersonationLevel}},
    @{'Name' = 'IsAuthenticated'; 'Expression' = {$_.IsAuthenticated}},
    @{'Name' = 'IsGuest'; 'Expression' = {$_.IsGuest}},
    @{'Name' = 'IsSystem'; 'Expression' = {$_.IsSystem}},
    @{'Name' = 'IsAnonymous'; 'Expression' = {$_.IsAnonymous}}
)


if ($IncludeMemberOf) {
    $SelectProperty += @{'Name' = 'MemberOf'; 'Expression' = {
            $_.Groups | Sort-Object -Property Value | Select-Object -Property @(
                @{'Name' = 'Name'; 'Expression' = {(New-Object System.Security.Principal.SecurityIdentifier($_)).Translate([System.Security.Principal.NTAccount])}},
                @{'Name' = 'SID'; 'Expression' = {$_.Value}}
            )
        }}
}

[Security.Principal.WindowsIdentity]::GetCurrent() | Select-Object -Property $SelectProperty