My boss wants to know group membership of AD users in the company. I have 3000+ users account to manage.
I wanted to write a bit of PowerShell code to get this, but without success.
I'd like to input user list from a .csv file. Its format is:
logindid first.user second.user third.user
Code I currently have:
$path = Split-Path $script:MyInvocation.MyCommand.Path
Import-Module ActiveDirectory
foreach ($UserName in (Import-CSV -Delimiter "`t" -Path $path\users.csv)) {
$strUserName = Get-ADUser -Filter 'SamAccountName -like "$username.loginid"' |
Select-Object Name, SamAccountName
Add-Content -Path $path\Inventaire.txt -Value $UserName
$StrUser = Get-ADPrincipalGroupMembership $strUserName |
select Name >> $path\Inventaire.txt
$StrUser.MemberOf >> $path\Inventaire.txt
}
I'm trying to get a list of my AD users and which group they are member of (from the Get-ADPrincipalGroupMembership
cmdlet). However, it seems the $strUserName
variable is empty.