Variable issue

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 1
Joined: Thu Nov 09, 2017 3:05 am

Variable issue

Post by barretsmile » Thu Nov 09, 2017 3:08 am

Probably a very simple resolution.
I would like to get the alias for users whos name starts with A,B or C from the get-mailbox command in Exchange. Then I want to take those users and add them to a group. Generally, Ive done this sort of thing with CSVs but id like to avoid that in this instance.
I can create a variable as below:
$AC = Get-Mailbox -RecipientTypeDetails UserMailbox -OrganizationalUnit $OUMbx | where {$_.DisplayName -Like "[A-C]*"}
When I run $AC, it does indeed list my users as expected.
Then I tried:
Get-ADUser Group-AC | Add-ADGroupMember -Members $AC
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
I always thought I could do $AC.Alias to get the aliases for example but that does not work.
Any ideas?

User avatar
cody m
Site Admin
Posts: 123
Joined: Thu Jun 01, 2017 7:20 am

Re: Variable issue

Post by cody m » Thu Nov 09, 2017 11:20 am

My guess would be that since the members parameter accepts a set of user, group, and computer objects, rather than an array of users, you need to loop through each member that is stored in your $AC variable.
Cody McCants
SAPIEN Technologies, Inc.

User avatar
Posts: 13015
Joined: Tue May 22, 2007 9:57 am

Re: Variable issue

Post by jvierra » Thu Nov 09, 2017 11:43 am

I already gave you the answer to that question.

Code: Select all

$AC = Get-Mailbox -RecipientTypeDetails UserMailbox -OrganizationalUnit $OUMbx | 
      where {$_.DisplayName -match '^[A-C]'}
You cannot use LIKE to match a RegEx pattern.

Also you cannot add a mailbox t]o an AD Group. Get the ADUser and add that.

See: ... powershell