# Add-DistributionGroupMember with variable

**URL:** <https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656>\
**Category:** PowerShell Help\
**Created:** [January 23, 2015, 4:10am UTC](https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656 "2015-01-23T04:10:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![byron-grogan](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@byron-grogan](https://forums.powershell.org/u/byron-grogan)\
**Post date:** [January 23, 2015, 4:10am UTC](https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656/1 "2015-01-23T04:10:04Z")

</div>

Is it possible to add members to a DistributionGroup using a variable. Importing a CSV file works but can be time consuming.

variable

```
$users = Get-Mailbox -Filter {RecipientTypeDetails -eq "UserMailbox"}
```

Adding the members, if I try to pipe foreach after the Add-DistributionGroupMember cmdlet the -Member parameter is missing.

```
PS C:\Windows\System32\WindowsPowerShell\v1.0> $users | foreach {Add-DistributionGroupMember -Identity EveryoneGroup -Member $_.users}
Cannot validate argument on parameter 'Member'. The argument is null. Supply a non-null argument and try the command again.
    + CategoryInfo : InvalidData: (:) [Add-DistributionGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Add-DistributionGroupMember
    + PSComputerName : outlook.office365.com
```

I’m probably doing something very stupid here…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [January 23, 2015, 4:15am UTC](https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656/2 "2015-01-23T04:15:53Z")

</div>

I suspect you’re just accessing the wrong property name inside your ForEach-Object block; “Users” seems like an unlikely name for a property on the output from Get-Mailbox.

Based one what I can see of the documentation for these cmdlets on TechNet, I think $\_.UserPrincipalName should work. (I don’t have an Exchange environment up at the moment where I can test that.)

---

<div class="post-metadata">

**Author:** ![byron-grogan](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@byron-grogan](https://forums.powershell.org/u/byron-grogan)\
**Post date:** [January 23, 2015, 5:27am UTC](https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656/3 "2015-01-23T05:27:51Z")

</div>

Sorry, total lack of comprehension on my part…Your right, your suggestion worked! Thank you

```
PS C:\Windows\System32\WindowsPowerShell\v1.0> $users | foreach {Add-DistributionGroupMember -Identity EveryoneGroup -Member $_.UserPrincipalName}
```

---

<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:46pm UTC](https://forums.powershell.org/t/add-distributiongroupmember-with-variable/3656/4 "2024-05-16T20:46:30Z")

</div>

Closed on May 16, 2024, 8:46pm UTC


