locked
Identify AD users by email address instead of username RRS feed

  • Question

  • Import-Csv -Path C:\users\user\desktop\test.csv |
        ForEach-Object {Set-ADUser -Identity $_.user -Replace @{UID = $_.UID}}

    I have the following script working great for my student users.

    However, our staff users are being exported from a different system that have separate usernames.

    (example: ad user = bmcalister | email = bmcalister@sd27j.net | IC username = mcalisterb)

    IC is the import source.

    Is there a way if I add email addresses into the CSV to identify the users via email address in AD to edit the UID attribute?

    Thanks in advance! 

    Tuesday, June 26, 2018 9:58 PM

Answers

  • Add the email and filter for it.
    Import-Csv -Path C:\users\user\desktop\test.csv |
        ForEach-Object{
            if($user = Get-AdUser -Filter "mail -eq '$($_.email)'"){
                Set-ADUser $user -Replace @{UID=$_.UID}
            }else{
                Write-Host 'Not found'
            }
        }
    


    \_(ツ)_/

    Tuesday, June 26, 2018 11:04 PM

All replies

  • Hi Brad

    You can't use a filter with set-aduser but you can use get-aduser and then pipe that to set-aduser.  If you add email to your csv file and then create a variable for mail and use that as a filter to search. Below should work. 

    Import-Csv -Path C:\users\user\desktop\test.csv
    ForEach-Object {
    $mail = $_.email
    Get-ADUser -Filter "mail -eq '$email'" |  Set-ADUser -Replace @{UID = $_.UID}}


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.



    Tuesday, June 26, 2018 11:00 PM
  • Add the email and filter for it.
    Import-Csv -Path C:\users\user\desktop\test.csv |
        ForEach-Object{
            if($user = Get-AdUser -Filter "mail -eq '$($_.email)'"){
                Set-ADUser $user -Replace @{UID=$_.UID}
            }else{
                Write-Host 'Not found'
            }
        }
    


    \_(ツ)_/

    Tuesday, June 26, 2018 11:04 PM