Get-ADuser and formatting results

What Im looking to do is to output all of my AD Users, including all of their properties, and then output that to a tabular format. The issue I am having is that some of the fields, like MemberOf, dont come through. My script looks like the following:

Get-ADuser -Filter * -Properties * | Export-CSV C:\Temp\MyFile.csv

This is almost what I want, but I just need for all of the properties to be expanded. Some end in "..." meaning there is more to be shown, and others such as "MemberOf" show "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" instead of showing the actual groups.

Thanks in advance for any help!

May 23rd, 2012 9:43pm

There is a way to do this with hash tables, that I don't quite have a grasp on, but here is how I accomplished this sort of thing when I wanted to expand the proxyaddresses attribute, which is a multi-valued attribute containing multiple smtp addresses:

http://mikecrowley.wordpress.com/2012/04/16/exchange-proxy-address-alias-report/

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2012 12:07am

Hi,

In addition, please try to use the below code to find all memberships of users:

Get-ADUser -filter * -Properties memberof | select -ExpandProperty memberof, name

Regards,

Yan Li

May 28th, 2012 4:50am

Yan, that is not a valid syntax.  You cannot use expandproperty with one item while simply passing another unexpanded.  When you do, the following error is returned:

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported.
At line:1 char:67
+ Get-ADUser -filter * -Properties memberof | select -ExpandProperty <<<<  memberof, name
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SelectObjectCommand

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2012 1:17pm

What you will need to do is decide how you want the information to be presented, and which AD attributes (properties) you want to be expanded in your csv. Basically you have two options how you can expand information:

  • Single fields, possibly with a delimiter. This keeps all the groups on a single column named MemberOf
  • Multiple fields, this would create a new column for each group a user is a member of. You would have to change the naming convention to something along the lines of MemberOf001,MemberOf002,MemberOf003...

Furthermore you have to decide which objects you want to expand, since there are multiple arrays returned when you use Get-ADUser, try this line of code to establish which properties returned by Get-ADUser use arrays and how many objects these arrays contain:

get-aduser jarrod -property * | % {$_.psobject.properties | ? {$_.value.count -ne $null} | % {$_.name,$_.value.count -join ":"}}

May 28th, 2012 2:41pm

Hi,

Thank you Mike for your pointing out my mistake.

Yes, we could not expand the name property. So I think we could save memberof property in an array and then add all those properties together to a CSV file.

Regards,

Yan Li

Free Windows Admin Tool Kit Click here and download it now
May 29th, 2012 1:31am

Right.  The link I posted above does this.  Additionally, Jaap has supplied another approach.
May 29th, 2012 3:32am

I had the same question and the "Exchange Proxy Address (alias) Report" Blog entry helped me a lot in this case.

Here is my adaption

$multipcgroups = @()

 $Pclist = import-csv mypclist.csv | foreach {get-adcomputer -identity $_.name -Properties * | select name, memberof}

 foreach ($pc in $pclist) {

 [array]$pcgroups = $pc.memberof

$ErrorActionPreference = 'SilentlyContinue'

 $pcadgroup = New-Object PSObject -Property @{

Name = $pc.name

 pcadgroup0 = $pcgroups[0] -replace "OU=SW,OU=Groupx,OU=foo,DC=company,DC=de" -replace "OU=Filter,OU=Technical Roles,DC=company,DC=de"

pcadgroup1 = $pcgroups[1] -replace "OU=SW,OU=Groupx,OU=foo,DC=company,DC=de" -replace "OU=Filter,OU=Technical Roles,DC=company,DC=de"

pcadgroup2 = $pcgroups[2] -replace "OU=SW,OU=Groupx,OU=foo,DC=company,DC=de" -replace "OU=Filter,OU=Technical Roles,DC=company,DC=de"

}

$ErrorActionPreference = 'Continue'

 $pcadgroupCount = ($pcgroups).count

 if ($pcadgroupCount -gt 0) {

$multipcgroups += $pcadgroup

}

}

 $multipcgroups | select name, pcadgroup0,pcadgroup1,pcadgroup2 | Export-CSV pcadgroups.csv -notype

regards

Andreas

Free Windows Admin Tool Kit Click here and download it now
May 28th, 2013 11:29am

The problem is that "MemberOf" is going to return as an array, so it has to be handled as such. What you want to do with the data afterwards is up to you.. but a rudimentary way to get this info would be:


$Users = Get-ADUser -filter * -Properties memberof 
ForEach($User in $Users)
   {
	$Groups = $User.Memberof
	write-host $User.Name
	write-host "___________________________"
	ForEach($Group in $Groups)
	{	 
	 Write-host $Group
	}
	
   }

You will end up getting the CN of the groups returned, but you will get them. 
December 4th, 2013 6:59pm

Try this:

| select userprincipalname, samaccountname, employeeNumber, @{name="MemberOf";expression={$_.memberof -join "`n"}} | Export-csv -path

Free Windows Admin Tool Kit Click here and download it now
September 16th, 2014 9:10pm

Sorry to necro an old thread, I just found an interesting way to do it using both get-aduser and get-qaduser (since the quest tool automatically formats array responses as text strings):

$_csv = @()
$_csv_header = """SamAccountName"",""LastLogon"",""MemberOf"""
$_csv += $_csv_header

$users = get-aduser -filter {samaccountname -like '*_Service'}
foreach ($user in $users){
    $entry = get-qaduser $user.SamAccountName
    $usersamaccountname = $entry.samaccountname
    $userLastLogon = $entry.LastLogon
    $usermemberof = $entry.MemberOf
    $_csv += """$usersamaccountname"",""$userlastlogon"",""$usermemberof"""
    }

$_csv | out-file c:\temp\serviceaccount.csv -Encoding ASCII


November 19th, 2014 6:11pm

I know its an old thread however if you want to do this in one command use select-object. For example OtherPager is an array and the following will export to csv as string.

Get-ADUser -Filter * -Properties samaccountname,otherPager | select-object  samaccountname, @{n='otherpager';e={$_.otherpager -join "`n"}} | export-csv c:\csv\csv.csv

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 4:59pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics