As for …
Apparently Get-ADPrincipalGroupMembership is not the way
… that's not really a valid statement and there are several Q&A's on your use case on this very site. Basically, it's just something like this.
# Get users with base properties and their group membership, display user and group name
ForEach ($TargetUser in (Get-ADUser -Filter *))
{
"`n" + "-"*12 + " Showing group membership for " + $TargetUser.SamAccountName
Get-ADPrincipalGroupMembership -Identity $TargetUser.SamAccountName | Select Name
}
# Results
------------ Showing group membership for Administrator
Name
----
Domain Users
Administrators
...
------------ Showing group membership for Guest
Domain Guests
Guests
Update for OP
I used the cmdlet to explain what you were using.
If you are on PowerShell v5, there are already local group cmdlets for this.
Get-Command -Name *LocalUser*
# Results
CommandType Name
Cmdlet Disable-LocalUser
Cmdlet Enable-LocalUser
Cmdlet Get-LocalUser
Cmdlet New-LocalUser
Cmdlet Remove-LocalUser
Cmdlet Rename-LocalUser
Cmdlet Set-LocalUser
Get-Command -Name *LocalGroup*
# Results
CommandType Name
Cmdlet Add-LocalGroupMember
Cmdlet Get-LocalGroup
Cmdlet Get-LocalGroupMember
Cmdlet New-LocalGroup
Cmdlet Remove-LocalGroup
Cmdlet Remove-LocalGroupMember
Cmdlet Rename-LocalGroup
Cmdlet Set-LocalGroup
Then doing something like this...
Clear-Host
$LocalUserName = Read-Host -Prompt 'Enter a username'
# If no user is passed, list all
If($LocalUserName -eq '')
{
ForEach($GroupName in Get-LocalGroup)
{
Get-LocalGroupMember -Group "$($GroupName.Name)" |
Select @{n='GroupName';e={$($GroupName.Name)}},Name
}
}
Else
{
# process only the user passed
Get-LocalGroup |
%{
If(Get-LocalGroupMember -Group "$($_.Name)" -Member $LocalUserName -ErrorAction SilentlyContinue)
{
[PSCustomObject]@{
GroupName = $_.Name
Username = $LocalUserName
}
}
}
}
If you are on lower versions, the you can use the PowerShellGallery.com module, or use ADSI directly. There are lots of articles and samples for this on this site and all over the web.
Example:
LocalUserManagement 3.0
a module that performs various local user management functions
See also:
Managing Local User Accounts with PowerShell - Part 1