none
Powershell AD export users and groups RRS feed

  • Question

  • I am trying to export AD users to a CSV using Powershell - this is the script I have been using:

    $ADGroups = Get-ADGroup -Filter '*' $Results = ForEach ($AdGroup in $ADGroups) { $Members = Get-AdGroupMember -Identity $AdGroup.SamAccountName ForEach ($Member in $Members) { $Hash = @{ Group = $AdGroup.SamAccountName Member = $Member.SamAccountName } New-Object -TypeName PSObject -Property $Hash } } $Results | Export-Csv -Path C:\output\groups.csv -NoTypeInformation

    instead of getting the SamAccountName, I would like to display the cn or displayName.

    Can anyone provide me with some insight as to what command I need to change to make this happen?  I have tried to just sub out cn or displayName for the SamAccountName and it only leads to errors.  I have not used PS very much.

    Tuesday, January 22, 2019 7:47 PM

Answers

  • Try this

    $ADGroups = Get-ADGroup -Filter * $Results = foreach ($AdGroup in $ADGroups) { $Members = Get-AdGroupMember -Identity $AdGroup.SamAccountName foreach($Member in $Members) { $Hash = @{ Group = $AdGroup.SamAccountName Member = (Get-ADUser $Member.SamAccountName -properties *).Displayname } New-Object -TypeName PSObject -Property $Hash } }


    Cheers,

    VJ

    Blog: https://itbigbang.co.uk/ 

    • Marked as answer by John Forward Thursday, January 24, 2019 2:32 PM
    Wednesday, January 23, 2019 9:59 AM

All replies

  • Change $member.Samaccountname to $member.displayname under $hash

    Cheers,

    VJ

    Blog: https://itbigbang.co.uk/ 


    Tuesday, January 22, 2019 7:58 PM
  • Hi VJ,  I made that change, but the member column in the csv export is blank.  Thoughts on why its not populating?
    Tuesday, January 22, 2019 8:34 PM
  • First it is critical that you learn to write correctly formatted code and that you only post code examples beyond one line using the code posting tool provided.

    Properly written your code works.

    $ADGroups = Get-ADGroup -Filter * 
    $Results = foreach ($AdGroup in $ADGroups) {
        $Members = Get-AdGroupMember -Identity $AdGroup.SamAccountName
        foreach($Member in $Members) {
            $Hash = @{
                Group = $AdGroup.SamAccountName 
                Member = $Member.SamAccountName 
            } 
            New-Object -TypeName PSObject -Property $Hash 
        }    
    } 
    $Results | Export-Csv -Path C:\output\groups.csv -NoTypeInformation

    As posted it is a complete mess that can never be executed.


    \_(ツ)_/


    • Edited by jrv Tuesday, January 22, 2019 10:17 PM
    Tuesday, January 22, 2019 10:16 PM
  • Change $member.Samaccountname to $member.displayname under $hash

    Cheers,

    VJ

    Blog: https://itbigbang.co.uk/ 


    DisplayName is not part of the returned "member" object.


    \_(ツ)_/

    Tuesday, January 22, 2019 10:18 PM
  • Try this

    $ADGroups = Get-ADGroup -Filter * $Results = foreach ($AdGroup in $ADGroups) { $Members = Get-AdGroupMember -Identity $AdGroup.SamAccountName foreach($Member in $Members) { $Hash = @{ Group = $AdGroup.SamAccountName Member = (Get-ADUser $Member.SamAccountName -properties *).Displayname } New-Object -TypeName PSObject -Property $Hash } }


    Cheers,

    VJ

    Blog: https://itbigbang.co.uk/ 

    • Marked as answer by John Forward Thursday, January 24, 2019 2:32 PM
    Wednesday, January 23, 2019 9:59 AM
  • Thank you so much VJ, that is exactly what I needed!
    Thursday, January 24, 2019 2:32 PM