0

I have a list of users in a csv file, they are the displaynames. I wrote the following but all I get back is the displayname not the memberships what is needed?

$csv = Import-CSV "c:\users.csv"
foreach($user in $csv){ 
$Displayname = $user.displayname
Get-aduser -filter {displayname -eq $displayname} -Properties displayname,memberOf |`
select "Displayname","MemberOf" | Export-Csv "c:\temp\usersmembership.csv"
}
  • not sure if it matters but I get the exepcted result by doing the exact same thing but without the double quotes in the 'select' statement – Matt Jul 12 '17 at 23:03

1 Answers1

0

The MemberOf field is an object of type Microsoft.ActiveDirectory.Management.ADPropertyValueCollection that will not dump to CSV. You need to use a select statement that will calculate the contents of the feild as text. This line will get the contents (Which will be annoying distinguished names), look up the common names, convert the output to text, and replace newlines with commas.

Get-ADUser -filter {displayname -eq $displayname} -prop displayname,memberof `
  | select DisplayName,@{Name='Groups';Expression={$_ | Select -ExpandProperty MemberOf `
  | Get-ADGroup | select -ExpandProperty Name | Out-String | %{$_ -replace ("`n",",")}}} `
  | Export-Csv "c:\temp\usersmembership.csv"
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
Zach Bolinger
  • 304
  • 1
  • 6