I'm trying to get only enabled members which are in multiple groups but not sure how to achieve this in the format I want. My current script is as follows.
$GroupList = @('GroupA','GroupB'
)
$Groupusers = @()
foreach ($Groups in $GroupList)
{
$Groupusers += Get-ADGroupMember -Identity $Groups | select @{Name="Groups";Expression={$Groups}},name
}
$Groupusers | Export-Csv -Path "C:\Users\Me\Documents\users.csv" -NoTypeInformation -Encoding Unicode
The above script works and formats the output for what I am after but this also includes even the disabled users.
Output:
Group name
GroupA UserA
GroupB UserB
I have tried the following script, this gives me the all the enabled user but not in a format like the above script.
$groupname = @('GroupA', 'GroupB')
$Groupusers=@()
foreach ($group in $groupname)
{
$Groupusers += Get-ADGroupMember -Identity $group | ? {$_.objectclass -eq "user"}
}
$result= @()
foreach ($activeusers in $Groupusers)
{
$result += (Get-ADUser -Identity $activeusers | ? {$_.enabled -eq $true} | select Name, Enabled)
}
$result
How can I achieve the output above but with only enabled users? Thanks.