# foreach question

**URL:** https://forums.powershell.org/t/foreach-question/12891
**Category:** PowerShell Help
**Created:** [July 19, 2019, 7:58am UTC](https://forums.powershell.org/t/foreach-question/12891 "2019-07-19T07:58:25Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)
#### Post date: [July 19, 2019, 7:58am UTC](https://forums.powershell.org/t/foreach-question/12891/1 "2019-07-19T07:58:25Z")

</div>

Hi guys,

I would like to change a customattribute for all users in active directory, but script below is not working, what do i do wrong?

&nbsp;

```
$users = get-aduser -Filter * | select samaccountname

foreach ($user in $users)
{
Set-AdUser -Identity $user -Replace @{ customAttribute1="something" }
}
```

&nbsp;

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [July 19, 2019, 9:03am UTC](https://forums.powershell.org/t/foreach-question/12891/2 "2019-07-19T09:03:04Z")

</div>

> [@](#):
>
> but script below is not working, what do i do wrong?

That’s quite vague. How do you know it is not working? 😉 What’s the situation before? What happens when you onvoke this code? What’s the situation after?

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [July 19, 2019, 9:25am UTC](https://forums.powershell.org/t/foreach-question/12891/3 "2019-07-19T09:25:15Z")

</div>

You need to use `Select-Object -ExpandProperty SamAccountName`; `select samaccountname` (or more properly `Select-Object -Property SamAccountName` if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property – which is what `Set-ADUser` will need.

---

<div class="post-metadata">

### Author: ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)
#### Post date: [July 19, 2019, 11:48am UTC](https://forums.powershell.org/t/foreach-question/12891/4 "2019-07-19T11:48:56Z")

</div>

A one liner without foreach

```
Get-ADUser -Filter * -Properties customAttribute1 | Set-AdUser -Identity $user -Replace @{ customAttribute1="something" }
```

---

<div class="post-metadata">

### Author: ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)
#### Post date: [July 22, 2019, 6:58am UTC](https://forums.powershell.org/t/foreach-question/12891/5 "2019-07-22T06:58:38Z")

</div>

[quote quote=166990]A one liner without foreach

\<textarea class="ace\_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"\>\</textarea\>

1

2

Get-ADUser -Filter \* -Properties customAttribute1 | Set-AdUser -Identity $user -Replace @{ customAttribute1="something" }

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

[/quote]

This is not helpfull because i need to do it for all the users not per user.

---

<div class="post-metadata">

### Author: ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)
#### Post date: [July 22, 2019, 7:05am UTC](https://forums.powershell.org/t/foreach-question/12891/6 "2019-07-22T07:05:54Z")

</div>

[quote quote=166951]You need to use `Select-Object -ExpandProperty SamAccountName`; `select samaccountname` (or more properly `Select-Object -Property SamAccountName` if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property — which is what `Set-ADUser` will need.

[/quote]

Hi Joel, i think i do understand what you tell mee but i don’t know exaiclly what you mean. I replaced the code with

```
Select-Object -Property SamAccountName
```

But i still receive the same error message:

Cannot bind parameter Identity

&nbsp;

Any help for me?

&nbsp;

&nbsp;

&nbsp;

---

<div class="post-metadata">

### Author: ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)
#### Post date: [July 22, 2019, 7:09am UTC](https://forums.powershell.org/t/foreach-question/12891/7 "2019-07-22T07:09:31Z")

</div>

Set-ADUser here will be executed for each user from Get-ADUser cmdelt. What will be the value for customattribute ?

---

<div class="post-metadata">

### Author: ![danno](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@danno](https://forums.powershell.org/u/danno)
#### Post date: [July 22, 2019, 11:17am UTC](https://forums.powershell.org/t/foreach-question/12891/8 "2019-07-22T11:17:50Z")

</div>

In your loop the way you have it, you can use the propertyname.

&nbsp;

```
$users = Get-ADUser -Filter *

foreach ($user in $users)
{
  Set-AdUser -Identity $user.samAccountName -Replace @{ customAttribute1="something" }
}
```

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [July 22, 2019, 11:46am UTC](https://forums.powershell.org/t/foreach-question/12891/9 "2019-07-22T11:46:59Z")

</div>

Yep. Or use `-ExpandProperty` (and _not_ `-Property`) like I mentioned in the initial one. 🙂

---

<div class="post-metadata">

### Author: ![pauljan1984](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pauljan1984](https://forums.powershell.org/u/pauljan1984)
#### Post date: [July 23, 2019, 2:51am UTC](https://forums.powershell.org/t/foreach-question/12891/10 "2019-07-23T02:51:13Z")

</div>

> [@](#):
>
> > [Joel /u/ta11ow @vexx32 wrote:](https://powershell.org/forums/topic/foreach-question/#post-166951)
> > You need to use `Select-Object -ExpandProperty SamAccountName`; `select samaccountname` (or more properly `Select-Object -Property SamAccountName` if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property — which is what `Set-ADUser` will need.
> 
> Hi Joel, i think i do understand what you tell mee but i don't know exaiclly what you mean. I replaced the code with
> \<textarea class="ace\_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"\>\</textarea\>
> 
> 1
> 
> Select-Object -Property SamAccountName
> 
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
> 
> But i still receive the same error message:
> 
> Cannot bind parameter Identity
> 
> Any help for me?

Thanks Dan! This is the way go, script working.

---

<div class="post-metadata">

### Author: ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)
#### Post date: [July 23, 2019, 9:22am UTC](https://forums.powershell.org/t/foreach-question/12891/11 "2019-07-23T09:22:46Z")

</div>

That one-liner should work without the “-Identity $user” after “Set-Aduser”.

---

<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:31pm UTC](https://forums.powershell.org/t/foreach-question/12891/12 "2024-05-16T20:31:55Z")

</div>


