SOLVED

How to copy one attribute to another

Brass Contributor

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...

4 Replies

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}
} 

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?

best response confirmed by Jesper Stein (Brass Contributor)
Solution

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]}
}
}

 

Worked great. Thanks for your help :)

1 best response

Accepted Solutions
best response confirmed by Jesper Stein (Brass Contributor)
Solution

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]}
}
}

 

View solution in original post