Powershell list users with filtered groups

I want to list all users with filtered AD groups, so I want all groups beginning with BLOG.
I've got a CSV with the name of all users.

I've tryed this code:
$users = Import-Csv 'C:\temp\old farm\users.csv' -Delimiter ";"

$Report = @()

foreach ($user in $users){
    $name  = $user.name

    $gebruiker = get-aduser -Filter {(displayname -like $name) -AND (enabled -eq $true)} -Properties *
    $sam = $gebruiker.SamAccountName
    $usergroupcollection = Get-ADPrincipalGroupMembership $sam | ? {$_.name -like "blog*"}
    
   
    
    $usergroupmembership = @()

    foreach ($UserGroup in $usergroupcollection) {
        $GroupDetails = Get-ADGroup -Identity $UserGroup
        $usergroupmembership += $GroupDetails.Name
    }

    $groups = $usergroupmembership -join ','

    $out = New-Object PSObject
    $out | Add-Member -MemberType NoteProperty -Name Name -Value $gebruiker.Name
    $out | Add-Member -MemberType NoteProperty -Name Sam -Value $gebruiker.SamAccountName
    $out | Add-Member -MemberType NoteProperty -Name Groups -Value $groups

    $report += $out
}

$Report | Sort-Object Name | FT -AutoSize

Open in new window


But there is an error on this
Knipsel.PNG
Has anyone a solution for this please?
Triforce2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
The solution is technically rather easy; stop using Name/DisplayName to identify AD users. the DisplayName you're filtering for is not guaranteed to be unique, and you have at least one Name/DisplayName in your csv that matches more than one account.
Use SamAccountName, UserPrincipalName, or the DistinguishedName to uniquely identify users.
Then you have a column "Name" in your csv, which you use in the query to find users with a DisplayName matching said Name, but you're returning a new column "Name", now with the actual Name of the AD Object, which might be totally different from the respective Name in your input csv. That's confusing at best, as there is no real relation between the input csv and the output. You should change the input column to "DisplayName".
Example: AD user with a Name of "Doe, John" and a DisplayName of "John Doe". If your csv "Name" column has "John Doe" in it, it will find the user based on its DisplayName, and return it with the column Name now as "Doe, John".
The case if an input name can not be found in AD is not properly handled.
And there's no need to retrieve the AD group - everything you return (which is only the group's Name) is already present in the objects returned by Get-ADPrincipalGroupMembership.
Finally, adding the "FT -AutoSize" directly in the script prevents you from exporting the script's output to a csv.
Try it like this; it still uses the "Name" column - you'll find it as DisplayName in the output, while the former output "Name" is now called "AD Name".
You can pipe the script's output to "ft -au", or "Out-GridView", or "Export-Csv", or whatever you need for further processing.
$users = Import-Csv 'C:\temp\old farm\users.csv' -Delimiter ';'

$users | ForEach-Object {
	If ($adUsers = Get-ADUser -Filter "(displayname -like '$($_.Name)') -and (enabled -eq 'true')" -Property DisplayName) {
		$adUsers | Select-Object -Property `
			@{n='Sam'; e={$_.SamAccountName}},
			DisplayName,
			@{n='AD Name'; e={$_.Name}},
			@{n='Groups'; e={(Get-ADPrincipalGroupMembership -Identity $_.SamAccountName | Where-Object {$_.Name -like 'blog*'} | Select-Object -ExpandProperty Name) -join ', '}}
	} Else {
		$_ | Select-Object -Property `
			@{n='Sam'; e={'<N/A>'}},
			@{n='DisplayName'; e={$_.Name}},
			@{n='AD Name'; e={'<N/A>'}},
			@{n='Groups'; e={'<N/A>'}}
	}
} | Sort-Object -Property Name

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chirag NagrekarSystem AnalystCommented:
What do you get when you print $sam ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.