# Set-ADUser multiple Identities not working

**URL:** https://forums.powershell.org/t/set-aduser-multiple-identities-not-working/15546
**Category:** PowerShell Help
**Created:** [January 28, 2021, 6:01pm UTC](https://forums.powershell.org/t/set-aduser-multiple-identities-not-working/15546 "2021-01-28T18:01:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sneebly](https://avatars.discourse-cdn.com/v4/letter/s/bb73d2/32.png) [@sneebly](https://forums.powershell.org/u/sneebly)
#### Post date: [January 28, 2021, 6:01pm UTC](https://forums.powershell.org/t/set-aduser-multiple-identities-not-working/15546/1 "2021-01-28T18:01:45Z")

</div>

Hey all, so trying to see if can get an extra hand on this…I am trying to pass a few variables into AD user attributes. Works for one user, but when add multiple users, it won’t pass separate objects. Not sure what I am missing…or better way to do this!

```auto
#Establishing Search Base OU
$Base = "OU=TST,OU=IT,DC=domain,DC=com

#Query and Set Values Replacement Attributes
$GivenName = (Get-ADUser -SearchBase $Base -Filter { (idautoPersonPreferredName -notlike "*") -and (idautoPersonPreferredLastName -notlike "*") -and (enabled -eq $true) } -Properties GivenName).GivenName; 
$SurName = (Get-ADUser -SearchBase $Base -Filter { (idautoPersonPreferredName -notlike "*") -and (idautoPersonPreferredLastName -notlike "*") -and (enabled -eq $true) } -Properties Surname).Surname;
$User = (Get-ADUser -SearchBase $Base -Filter { (idautoPersonPreferredName -notlike "*") -and (idautoPersonPreferredLastName -notlike "*") -and (enabled -eq $true) } -Properties "Name").Name;

#Set User Attributes
ForEach-Object {
Set-ADUser -Identity "$User" -Replace @{ idautoPersonPreferredName="$GivenName";idautoPersonPreferredLastName="$SurName" }
}

```

---

<div class="post-metadata">

### Author: ![andysvints](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/andysvints/32/1017_2.png) [@andysvints](https://forums.powershell.org/u/andysvints)
#### Post date: [March 5, 2021, 9:03pm UTC](https://forums.powershell.org/t/set-aduser-multiple-identities-not-working/15546/2 "2021-03-05T21:03:18Z")

</div>

Hello @sneebly,  
One of the possible way to do it will be something like following:

```auto
#Establishing Search Base OU
$Base = "OU=TST,OU=IT,DC=domain,DC=com"

#Query and Set Values Replacement Attributes
$User = (Get-ADUser -SearchBase $Base -Filter { (idautoPersonPreferredName -notlike "*") -and (idautoPersonPreferredLastName -notlike "*") -and (enabled -eq $true) } -Properties Name,Surname,GivenName)

#Set User Attributes
foreach ($item in $User)
{
    Set-ADUser -Identity $item.Name -Replace @{ idautoPersonPreferredName=$($item.GivenName);idautoPersonPreferredLastName=$($item.Surname)}
}

```

Hope that help.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/set-aduser-multiple-identities-not-working/15546/3 "2024-05-16T20:27:46Z")

</div>


