none
A script to pull manager email addresses from a list of users RRS feed

  • Question

  • Hello all,

    I am trying to pull a list of managers from a csv list of usernames. I can do it with two separate foreach loops and some text file massaging in between stages. I would like to get it to the point that I can get the email addresses as output without needing to do the in between text file cleanup.

    $b = Import-Csv "C:\temp\userlist\accountaccess" -header ("samaccountname")
    foreach ($user in $b) {
    get-aduser $user.samaccountname -properties manager|Select manager | out-file C:\temp\userlist\manager.txt -width 150 -append
    }


    this generates a list of users 


    manager                                                                                                                                               
    -------                                                                                                                                               
    CN=lastname1\, firstname1,OU=Users,DC=domain,DC=com                                 



    manager                                                                                                                                               
    -------                                                                                                                                               
    CN=lastname2\, firstname2,OU=Users,DC=domain,DC=com                                                                           

    I format that file down to a minimum:                                                                                                                                 

    "CN=lastname1\, firstname1,OU=Users,DC=domain,DC=com"
    "CN=lastname2\, firstname2,OU=Users,DC=domain,DC=com"

    then run this:

    $manager = Import-Csv "C:\temp\userlist\manager.txt" -header ("distinguishedname")
    foreach ($user in $manager) {
    get-aduser $user.distinguishedname -properties EmailAddress | select EmailAddress
    }

    And that gives me a list of email addresses ouput to the screen.

    I'd like to request assistance with merging those operations into one loop that outputs the text file containing the email addresses if thats possible.

    I was thinking along the lines of processing each users manager and getting the email address then appending that to a file, but I'm stuck as to how to get there.

    Thanks for any input,

    John

    Thursday, April 24, 2014 9:54 PM

Answers

  • If you only want a list of email addresses, then how about...

    Import-Csv "C:\temp\userlist\accountaccess.csv" -header ("samaccountname") | 
    % {Get-ADUser (Get-ADUser $_.samaccountname -properties manager).manager -properties emailaddress} | Select-Object emailaddress | 
    Export-CSV ".\managers.csv"


    • Marked as answer by Akhnot Friday, April 25, 2014 8:54 PM
    Friday, April 25, 2014 3:03 PM

All replies

  • None of this will work with a CSV file the way you are doing it.

    What is in the CSV file?

    Import-Csv C:\temp\userlist\accountaccess
         ForEach-Object {
             get-aduser $user.samaccountname -properties manager, DisplayName |
                 %{
                      Get-Aduser $_  -properties EmailAddress |
                          Add-Member NoteProperty Username $user.DisplayName
                 }
         } |
         Select Name, DisplayName, EmailAddress  |
         Export-Csv outfile.csv
    
    

    This is how to get combined data in PowerShell assuming my guesses were correct about your file.


    ¯\_(ツ)_/¯

    Friday, April 25, 2014 12:12 AM
  • If you only want a list of email addresses, then how about...

    Import-Csv "C:\temp\userlist\accountaccess.csv" -header ("samaccountname") | 
    % {Get-ADUser (Get-ADUser $_.samaccountname -properties manager).manager -properties emailaddress} | Select-Object emailaddress | 
    Export-CSV ".\managers.csv"


    • Marked as answer by Akhnot Friday, April 25, 2014 8:54 PM
    Friday, April 25, 2014 3:03 PM
  • This was the simple one I was looking for!

    Thank you very much.

    John

    Friday, April 25, 2014 8:58 PM