locked
Set-AdUser -Identity help RRS feed

  • Question

  • import-csv C:\phoneusers.csv | ForEach-Object {
    $Iden = (get-aduser -LDAPFilter "(DisplayName=$_.user)" | select samaccountname)
    Set-ADUser -Identity $Iden -OfficePhone $_.ext}

    I have a csv with Display Names and extensions, -Identity does not accept Display Names. I am trying to make Powershell search for the samAccountName using the displayName and then feed it into the final line.

    My csv headers are "user" and "ext"

    Any suggestions would be greatly appreciated! Thank you!

    Friday, February 16, 2018 5:12 PM

Answers

  • Select-Object SamAccountName will give an object as an output, you should use the -ExpandProperty parameter which will give the output as string.

    $Iden = Get-ADUser -Filter "DisplayName -eq '$($_.user)'" | select -ExpandProperty samaccountname


    Regards kvprasoon


    Friday, February 16, 2018 5:39 PM

All replies

  • DisplayName is not unique.  You can get back more than one and get back the wrong one.

    Get-AdUser -Filter "DisplayName -eq '$($_.user)'"


    \_(ツ)_/

    Friday, February 16, 2018 5:19 PM
  • import-csv C:\phoneusers.csv | ForEach-Object {
    $Iden = (Get-ADUser -Filter "DisplayName -eq '$($_.user)'" | select samaccountname)
    Set-ADUser -Identity $Iden -OfficePhone $_.ext}

    I have a csv with Display Names and extensions, but -Identity does not accept Display Names. I am trying to make Powershell search for the samAccountName using the displayName and then feed it into the final line to change the phone field for that user.

    My csv headers are "user" and "ext"

    Any suggestions would be greatly appreciated! Thank you!

    • Merged by jrv Friday, February 16, 2018 6:58 PM DUPLICATE
    Friday, February 16, 2018 5:27 PM
  • Select-Object SamAccountName will give an object as an output, you should use the -ExpandProperty parameter which will give the output as string.

    $Iden = Get-ADUser -Filter "DisplayName -eq '$($_.user)'" | select -ExpandProperty samaccountname


    Regards kvprasoon


    Friday, February 16, 2018 5:39 PM
  • import-csv C:\phoneusers.csv |

    ForEach-Object {
    $Iden = (Get-ADUser -Filter "DisplayName -eq '$($_.user)'" -properties OfficePhone)
    $Iden.OfficePhone = $_.ext

    Set-ADUser -Instance $Iden}

    Friday, February 16, 2018 5:50 PM
  • And maybe $_.user not $($_.user) ?
    Friday, February 16, 2018 5:51 PM
  • And displayName is not even required to have a value. It can be missing.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, February 16, 2018 6:18 PM
  • This is correct, undoubtedly, but at the worst MVMSTech44 can run the script with verification.

    $userswithphone = import-csv C:\script\phoneusers.csv -Encoding UTF8 -Header user, phone
    $badusers = ""
    ForEach ($user in $userswithphone)
    {
        $DispName = $user.user
        $phonenumber = $user.phone
        $Iden = Get-ADUser -Filter "DisplayName -eq '$DispName'" -properties OfficePhone, DisplayName, SamAccountName
        $i = $Iden | Measure-Object
        switch ($i.Count)
        {
        1       { $Iden.OfficePhone = $phonenumber
                    Set-ADUser -Instance $Iden 
                    write-host "ALL OK     $DispName"  }
        0       { $message = "User with DisplayName '$DispName' does not exist!`r`n"
                    write-host $message
                    $badusers += $message }
        Default {
                        foreach ($u in $Iden)
                            {  $sam = $u.SamAccountName
                                $message = "$DispName $sam`r`n"
                                write-host $message
                                $badusers += $message  }
                    }
         }
             
    }

    $badusers > C:\script\badusers.txt
    Tuesday, February 20, 2018 12:06 PM