Managing Memberships in Bulk in PowerShell
Managing Memberships in Bulk in PowerShell
You might find that managing the membership of Distribution Groups in PowerShell is something you do more frequently than actually create the groups from scratch.
But we’ll look at both scenarios here. Let’s say that we need to create a group for finance users. So I’m going to do a new Distribution Group called “Finance.” I’m going to tab through these parameters here, and under type, what I’m going to say is “distribution.”
This is going to be a Distribution Group and Active Directory that’s mail‑enabled. That gets created. It gets created as universal.
What I want to do from there is add some users to that group. We have a bunch of different Cmdlets to manage Distribution Groups. What I’m going to do is run Get‑Mailbox and query the organizational unit, “Finance.”
I want to pull all these users that are all in this OU and dump them in that group.
This is how we can do some bulk administration of groups. So we’ve created our Finance group. From here, I want to take all of these users in the OU and put them in that group.
Luckily, we don’t have to do any loops for this. We don’t have to iterate over anything. We can simply make use of the pipeline because the Add Distribution Group Cmdlet gladly accepts input from anything sent over from Get‑Mailbox.
We can simply say add Distribution Group Member “Finance.”
Now, we don’t get anything back. That means that the command completed successfully.
To check that we could just do a Get‑DistributionGroup Member on Finance.
Now all of the users that we saw previously are actually there now.
There are other ways that you can filter on this information. We’re using one of the server-side parameters here to pull from an Organizational Unit (OU).
You could also do something like this where you’re piping to where object and maybe say well give me all the ones that the Office is equal to sales.
You might find that sometimes you got parameters on the actual get Cmdlet where you don’t have to do this late filtering. But that might be another way where you could continue piping down. Notice that Office isn’t a parameter here, so that’s something that you have to filter on. Just a couple of ideas to have the pipeline feed that add Get‑DistributionGroup Member Cmdlet.
Let’s look at one more example.
I’m going to do a new Distribution Group “Accounting,” and this time I’ll do it as a security group in Active Directory, that’s going to be mail‑enabled.
You can see that by looking at the group type information that comes back. So it’s a universal security group at this point. I can assign permissions on a NTFS share if I needed to on these group members. I’ll also send mail to this group.
Now that that group’s created I’m going to do something different this time. I’m going to run Get‑User and I’m going to say, “Recipient type details, user mailbox,” so it will only give me active directory users that have a mailbox.
I’m going to filter on a different property this time. I’m going to look for a title of accountant so $_.title ‑eq “Accountant.”
Let’s make sure that actually works first. Looks like that works just fine.
So these might be spread out multiple OU’s, running Get‑Mailbox and specifying the OU wouldn’t work in this case. I’m going to take that one liner and I’m going to continue piping over to add Distribution Group Member “Accounting.”
Take all those users and put them in that group.
No errors come back so we could just verify that by running GetDistributionGroupMember on “Accounting.”
And as you can see everything is there at that point.
One of the things to mention is that you can obviously modify these groups by running the Set‑DistributionGroup Cmdlet.
You can come in here and change sales and there are a number of parameters. You can enable moderation, do a number of things to modify these groups.
That’s how you provision and modify groups and also add multiple members into a group at one time.
You May Also Like
In this Office 365 training video, instructor Spike Xavier demonstrates how to create users and manage passwords in Office 365.
In this IT Security training video, Security expert Mike Danseglio (CISSP / CEH) will perform several malware investigations including rootkits, botnets, viruses, and browser toolbars.