Forum Discussion

Jesper Stein's avatar
Jesper Stein
Brass Contributor
Jun 07, 2017

How to copy one attribute to another

Hi

 

I need to move data from one attribute to another on a user object in Active Dirctory.

 

Today the attribute homePhone is porpulated with users private mobile number. I need to move this to otherMobile.

 

First I try to do this on my own account this way:

$homePhone = get-aduser -identity INITIALS -Properties * | Select-Object homePhone

 

Then I want to copy from homePhone to otherMobile this way:

set-aduser -identity INITIALS -add @{otherMobile='$homePhone')

 

This sort of works. In my otherMobile is now: {otherMobile='+45XXXXXXXX')

 

How can I only put in the value $homePhone and not the whole string?

 

Of course in the end the script should find all users with the homePhone porpulated and copy the value to otherMobile...

  • Hi
    Otherhomephone is multivalue attribute so that it is not working and you can try below script

    $users = get-aduser -Filter * -Properties *|Where-Object {$_.otherhomePhone -ne $null }| Select-Object Samaccountname,otherhomePhone

    foreach($user in $users)
    {

    if($user.otherhomePhone.count -gt 1)
    {

    foreach($hph in $user.otherhomePhone)
    {

    set-aduser -identity $user.Samaccountname -add @{otherMobile=$hph}
    }

    }
    else
    {
    set-aduser -identity $user.Samaccountname -add @{otherMobile=$user.otherhomePhone[0]}
    }
    }

     

  • Please use the below script and check.

     

    $users = get-aduser -Filter * -Properties *|Where-Object {$_.homePhone -ne $null }| Select-Object Samaccountname,homePhone
    
    foreach($user in $users)
    {
    set-aduser -identity $user.Samaccountname -add @{otherMobile=$user.homephone}
    } 
    
    
    • Jesper Stein's avatar
      Jesper Stein
      Brass Contributor

      Hi.

       

      That works fine. But I made a little error, it was the attribute otherhomePhone and not homePhone. When I replace homePhone with otherhomePhone, it does not work any more.

       

      Can you tell me why?

  • Hi
    Otherhomephone is multivalue attribute so that it is not working and you can try below script

    $users = get-aduser -Filter * -Properties *|Where-Object {$_.otherhomePhone -ne $null }| Select-Object Samaccountname,otherhomePhone

    foreach($user in $users)
    {

    if($user.otherhomePhone.count -gt 1)
    {

    foreach($hph in $user.otherhomePhone)
    {

    set-aduser -identity $user.Samaccountname -add @{otherMobile=$hph}
    }

    }
    else
    {
    set-aduser -identity $user.Samaccountname -add @{otherMobile=$user.otherhomePhone[0]}
    }
    }

     

Resources