Output Help

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Lee d Lee d 2 years, 3 months ago.

  • Author
    Posts
  • #24522
    Profile photo of Lee d
    Lee d
    Participant

    I have become decent at finding the AD data I need but when it comes to creating a spreadsheet with the information I find my knowledge totally inadequate to do what I want to do. I am hoping for some quick help and some helpful URL's.

    When I run "Get-aduser MyID -Properties * | FL Name, Memberof " the memberof field has over 100 entries in curly braces separated by commas.
    I would like to end up with a spreadsheet with my name in one column and then another column of the 100+ groups, then the next users name, his groups etc.

    I have looked at Format-List, and tried Exportto-csv with no luck, what is the best way to do this?

    Thanks for any help you can provide,
    Lee

  • #24523
    Profile photo of Don Jones
    Don Jones
    Keymaster

    That's because MemberOf is a collection of objects, not just a simple value; when asked to display a collection in a list or table, PowerShell can't create a sub-list or sub-table, so it does the curly bracket thing.

    There's no straightforward way to get what you want using what's built into the shell. You'd have to code up something custom.

  • #24524
    Profile photo of Craig Duff
    Craig Duff
    Participant
    Get-aduser MyID -Properties memberof |
    Select-Object Name,@{n='groups';e={
    		$_.memberof -join "`r`n"
    	}} |
    Export-CSV -NoTypeInformation -Path c:\test.csv
    
    See if that is closer to what you want.
    • #24527
      Profile photo of Lee d
      Lee d
      Participant

      Thanks Don. I just purchased your PS in 30 days of lunches; does that include how I might handle this?

  • #24525
    Profile photo of Craig Duff
    Craig Duff
    Participant
    Get-aduser MyID -Properties memberof |
    ForEach-Object {
    	$name = $_.name
    	$_.memberof |
    	ForEach-Object {
    		[pscustomobject]@{
    			'Name'=$name
    			'Group'=$_
    		}
    	}
    } |
    Export-Csv -NoTypeInformation -Path c:\test.csv
    
    Or something like this.
    • #24528
      Profile photo of Lee d
      Lee d
      Participant

      Output has a little extra info, how do I trim it down?

      Name Dan, Lee
      Group CN=g-cae-NetworkEng-readers,OU=Roles,DC=up,DC=corp,DC=upc
      Name Dan, Lee
      Group CN=g-jar-read,OU=Roles,DC=up,DC=corp,DC=upc

  • #24526
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Ugly, but it works...

    Calculated Expression
    Loop through MemberOf
    Split the CN into an array
    Get the first index (e.g. CN=MyGroup)
    Replace CN= with nothing, basically strip off CN=
    And finally Join the returned array with a semicolon...

    Get-ADUser myuser -Properties MemberOf | Select Name, @{Label="Groups";Expression={$($_.MemberOf | foreach{($_.Split(",")[0].Replace("CN=",""))}) -Join ";"}}

    Returns:

    Name        Groups                                                                         
    ----        ------                                                                         
    My User Group1;Group2;Group3;Group4;...
    

You must be logged in to reply to this topic.