# Can't get pipeline variable to evaluate

**URL:** <https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095>\
**Category:** PowerShell Help\
**Created:** [October 15, 2020, 10:39am UTC](https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095 "2020-10-15T10:39:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [October 15, 2020, 10:39am UTC](https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095/1 "2020-10-15T10:39:54Z")

</div>

Hi.

I would like to write the SamAccountName property of an account into the mailNickname property.

1 nice simple string…

```
Get-ADUser mdawson -Properties SamAccountName,Mail,MailNickName | Where-Object {($_.mail -ne $null) -and ($_.mailNickName -eq $null)} | Set-Aduser -Replace @{MailNickName = "$_.SamAccountName"}
```

I can’t for the life of me work out how to get the final Set-Aduser cmdlet to work, it fails to expand the $\_.SamAccountName parameter and I end up with mailNickname being equal to “.SamAccountName”

Any ideas?

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![adminofthings](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@adminofthings](https://forums.powershell.org/u/adminofthings)\
**Post date:** [October 15, 2020, 10:54am UTC](https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095/2 "2020-10-15T10:54:18Z")

</div>

$\_ is the current object in a pipeline. The current object concept exists when piping to a command that evaluates a script block. You are accessing $\_.SamAccountName outside of a script block. A simple workaround for you is to pipe into Foreach-Object:

```
Get-ADUser mdawson -Properties SamAccountName,Mail,MailNickName |
    Where-Object {($_.mail -ne $null) -and ($_.mailNickName -eq $null)} | Foreach-Object { 
        Set-Aduser -Replace @{MailNickName = $_.SamAccountName}
}
```

You should also not use quotes around $_.SamAccountName. This will cause $_ to be expanded and then .SamAccountName will be added to the end of the expanded string. Quotes in this fashion can only be used with simple variable references. Your options are to not use quotes at all or use quotes around a sub-expression “$($\_.SamAccountName)”.

---

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [October 20, 2020, 9:50am UTC](https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095/3 "2020-10-20T09:50:57Z")

</div>

Thank you very much. I knew it would be easy.

---

<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:29pm UTC](https://forums.powershell.org/t/cant-get-pipeline-variable-to-evaluate/15095/4 "2024-05-16T20:29:11Z")

</div>


