none
Exporting Multiple AD users to excel

    Question

  • Hi All

    i have an excel sheet with around 100users in there. i would like to get OU information of all users in that excel list and export it to an excel via powershell command. thanks in advance.


    Imran A Memon

    Thursday, March 23, 2017 5:21 AM

Answers

  • save a CSV file with header samaccountname of all the users.

    EX:-

    Samaccountname

    User1

    User2

    Use this script to accomplish the task that you are looking for, also you can add as many attributes as you want to the script.

    Import-Module Activedirectory
    
    $a = Import-Csv .\myusers.csv
    $c =@()
    foreach ($b in $a)
    {
    $abc = Get-ADUser $b.samaccountname
    
    
    $Distinguishedname= $abc.distinguishedname
    $sam =$abc.samaccountname
    
    
    
    $obj = new-object psobject
    
    $obj | add-member -MemberType noteproperty -Name Smaccountname -Value $sam
    $obj | add-member -MemberType noteproperty -Name Distinguishedname -Value $Distinguishedname
    
    
    
    $c+=$obj 
    }
    $c
    $c | Export-Csv .\NewData.csv -notypeinformation 
    

    • Marked as answer by ImranAMemon Friday, March 24, 2017 5:12 AM
    Thursday, March 23, 2017 3:07 PM

All replies

  • Please post this in Powershell or scripting forum.

    You can refer https://social.technet.microsoft.com/Forums/scriptcenter/en-US/bfb08794-d27e-44ef-91c3-1412fe116e64/list-all-of-the-users-in-ou-then-export-to-csv?forum=ITCG

    This has the script to get the details for the users in entire domain.

    Thursday, March 23, 2017 12:13 PM
  • save a CSV file with header samaccountname of all the users.

    EX:-

    Samaccountname

    User1

    User2

    Use this script to accomplish the task that you are looking for, also you can add as many attributes as you want to the script.

    Import-Module Activedirectory
    
    $a = Import-Csv .\myusers.csv
    $c =@()
    foreach ($b in $a)
    {
    $abc = Get-ADUser $b.samaccountname
    
    
    $Distinguishedname= $abc.distinguishedname
    $sam =$abc.samaccountname
    
    
    
    $obj = new-object psobject
    
    $obj | add-member -MemberType noteproperty -Name Smaccountname -Value $sam
    $obj | add-member -MemberType noteproperty -Name Distinguishedname -Value $Distinguishedname
    
    
    
    $c+=$obj 
    }
    $c
    $c | Export-Csv .\NewData.csv -notypeinformation 
    

    • Marked as answer by ImranAMemon Friday, March 24, 2017 5:12 AM
    Thursday, March 23, 2017 3:07 PM
  • Just to clarify, your list of users should be the sAMAccountNames (pre-Windows 2000 logon names) of the users. The common names (also called the Relative Distinguished Names, or in ADUC simply the Name) does not uniquely identify the users and will not work with the Get-ADUser cmdlet. Also, the distinguishedName of each user is really the only attribute that reveals the parent OU (the OU where the user object resides in AD). Again, you can parse the distinguishedName for the Relative Distinguished Name of the OU, but this does not uniquely identify the OU.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, March 23, 2017 3:54 PM
  • Thank you. this worked like a charm.

    Imran A Memon

    Friday, March 24, 2017 5:12 AM