Skip to content

Instantly share code, notes, and snippets.

@curious-username
Created April 19, 2023 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-username/f6e25ef5add7306af827384dbfb39365 to your computer and use it in GitHub Desktop.
Save curious-username/f6e25ef5add7306af827384dbfb39365 to your computer and use it in GitHub Desktop.
read me

$ComputerName = $env:COMPUTERNAME $LoggedOnUsers = Get-WmiObject -Class Win32_LoggedOnUser -ComputerName $ComputerName

foreach ($User in $LoggedOnUsers) { $Domain = $User.Antecedent.Split('"')[1] $UserName = $User.Antecedent.Split('"')[3] $Groups = Get-ADPrincipalGroupMembership -Identity "$Domain$UserName" -ErrorAction SilentlyContinue | Select-Object Name

Write-Output "$Domain\$UserName"

if ($Groups) {
    Write-Output "Member of groups:"
    foreach ($Group in $Groups) {
        Write-Output "- $($Group.Name)"
    }
} else {
    Write-Output "User is not a member of any groups."
}

Write-Output ""

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment