SOLVED

How to copy one attribute to another

%3CLINGO-SUB%20id%3D%22lingo-sub-75643%22%20slang%3D%22en-US%22%3EHow%20to%20copy%20one%20attribute%20to%20another%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-75643%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20move%20data%20from%20one%20attribute%20to%20another%20on%20a%20user%20object%20in%20Active%20Dirctory.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EToday%20the%20attribute%20%3CSTRONG%3EhomePhone%3C%2FSTRONG%3E%20is%20porpulated%20with%20users%20private%20mobile%20number.%20I%20need%20to%20move%20this%20to%20%3CSTRONG%3EotherMobile%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20I%20try%20to%20do%20this%20on%20my%20own%20account%20this%20way%3A%3C%2FP%3E%3CP%3E%24homePhone%20%3D%20get-aduser%20-identity%20INITIALS%20-Properties%20*%20%7C%20Select-Object%20homePhone%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20I%20want%20to%20copy%20from%20%3CSTRONG%3EhomePhone%3C%2FSTRONG%3E%20to%20%3CSTRONG%3EotherMobile%3C%2FSTRONG%3E%20this%20way%3A%3C%2FP%3E%3CP%3Eset-aduser%20-identity%20INITIALS%20-add%20%40%7BotherMobile%3D'%24homePhone')%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20sort%20of%20works.%20In%20my%20%3CSTRONG%3EotherMobile%3C%2FSTRONG%3E%20is%20now%3A%20%7BotherMobile%3D'%2B45XXXXXXXX')%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHow%20can%20I%20only%20put%20in%20the%20value%3CSTRONG%3E%20%24homePhone%3C%2FSTRONG%3E%20and%20not%20the%20whole%20string%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOf%20course%20in%20the%20end%20the%20script%20should%20find%20all%20users%20with%20the%20%3CSTRONG%3EhomePhone%3C%2FSTRONG%3E%20porpulated%20and%20copy%20the%20value%20to%20%3CSTRONG%3EotherMobile%3C%2FSTRONG%3E...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-75643%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ejeste%40danishcrown.dk%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-76132%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20one%20attribute%20to%20another%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-76132%22%20slang%3D%22en-US%22%3E%3CP%3EWorked%20great.%20Thanks%20for%20your%20help%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-76112%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20one%20attribute%20to%20another%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-76112%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CBR%20%2F%3EOtherhomephone%20is%20multivalue%20attribute%20so%20that%20it%20is%20not%20working%20and%20you%20can%20try%20below%20script%3C%2FP%3E%3CPRE%3E%24users%20%3D%20get-aduser%20-Filter%20*%20-Properties%20*%7CWhere-Object%20%7B%24_.otherhomePhone%20-ne%20%24null%20%7D%7C%20Select-Object%20Samaccountname%2CotherhomePhone%3CBR%20%2F%3E%3CBR%20%2F%3Eforeach(%24user%20in%20%24users)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%20if(%24user.otherhomePhone.count%20-gt%201)%3CBR%20%2F%3E%20%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%20foreach(%24hph%20in%20%24user.otherhomePhone)%3CBR%20%2F%3E%20%7B%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20set-aduser%20-identity%20%24user.Samaccountname%20-add%20%40%7BotherMobile%3D%24hph%7D%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%20else%3CBR%20%2F%3E%20%7B%3CBR%20%2F%3E%20set-aduser%20-identity%20%24user.Samaccountname%20-add%20%40%7BotherMobile%3D%24user.otherhomePhone%5B0%5D%7D%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-76103%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20one%20attribute%20to%20another%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-76103%22%20slang%3D%22en-US%22%3E%3CP%3EHi.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20works%20fine.%20But%20I%20made%20a%20little%20error%2C%20it%20was%20the%20attribute%20otherhomePhone%20and%20not%20homePhone.%20When%20I%20replace%20homePhone%20with%20otherhomePhone%2C%20it%20does%20not%20work%20any%20more.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20tell%20me%20why%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-75670%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20copy%20one%20attribute%20to%20another%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-75670%22%20slang%3D%22en-US%22%3E%3CP%3EPlease%20use%20the%20below%20script%20and%20check.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24users%20%3D%20get-aduser%20-Filter%20*%20-Properties%20*%7CWhere-Object%20%7B%24_.homePhone%20-ne%20%24null%20%7D%7C%20Select-Object%20Samaccountname%2ChomePhone%0A%0Aforeach(%24user%20in%20%24users)%0A%7B%0Aset-aduser%20-identity%20%24user.Samaccountname%20-add%20%40%7BotherMobile%3D%24user.homephone%7D%0A%7D%20%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Highlighted
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
Highlighted

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

Highlighted

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?

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

 

Highlighted

Worked great. Thanks for your help :)