Say, you get an email asking to add a bunch of members to a distribution list you manage. What is the easiest way to do this? Going to Outlook, making 5 clicks to get to the dialog box, and then manually adding each user from the address book picker is definitely not fun. However, PowerShell definitely is.
I’ve seen people submitting lists of members to add in a couple of ways: separated by commas (or semicolons) or each on a separate line. Both would work fine – the only difference is how you would tell PowerShell to split this string into actual members’ names.
Let’s start with a comma-separated list. Like this:
“Hey Dmitry,
Could you please add Kirk Munro, Darin Pendergraft, Oleg Shevnin to the PowerGUI DL?
Thanks!“
All you need to do, is copy the part of the email with the names, use PowerShell -split
operator to break it by comma characters, and pipe the result into Add-QADGroupMember
:
'Kirk Munro, Darin Pendergraft, Oleg Shevnin' -split "," | Add-QADGroupMember PowerGUI
That is it!
Line by line option is not much different – you just have to split by newline character ("`n"
) instead of comma:
'Kirk Munro
Darin Pendergraft
Oleg Shevnin' -split "`n" | Add-QADGroupMember PowerGUI
And by the way, email addresses instead of user names are totally fine too:
'user1@domain.my, user2@domain.my' -split "," | Add-QADGroupMember PowerGUI
One of those cases when command line is so much easier than UI. π
Another best practice on powershell, i like it!
Will should be careful to use ‘Kirk Munro, Darin Pendergraft, Oleg Shevnin’ -split “,” because two people Darin Pendergraft and Oleg Shevnin will be starting from space.
You should use ‘Kirk Munro, Darin Pendergraft, Oleg Shevnin’ -split “, ” instead of
Mihail, actually in my experiments spaces were not an issue and Add-QADGroupMember was working just fine even with speces preceding the names. Did you have any issues?
No, i had no problems but when i piped out the output after split it was with space π
Yes, that for sure. Split leaves the spaces (unless you explicitly tell it not to :)) but then Add-QADGroupMember seems to be smart enough to handle them.
What about importing from a csv file?
This wouldn’t work:
Import-Csv C:\temp\list.csv -Delimiter “,” | ForEach-Object -process {Add-QADGroupMember Group}
Anyone?
Import-CSV produces objects with members called the way your columns are – not strings.
If your CSV file is a simple text file with a username per line, use get-content instead – this will give strings:
Get-Content C:\temp\list.txt | Add-QADGroupMember MyGroup
If you do have a CSV file and the username or email address or some other unique identifier as a column – use $_.ColumnName explicitly, e.g.:
Import-Csv C:\temp\list.csv -Delimiter ‘,’ | ForEach-Object -process {Add-QADGroupMember -Identity MyGroup -Member $_.Username}
If you still have trouble – please post your question to the forums at http://powergui.org
Dmitry,
Have task to automate task of adding users to group.
I have output file such as:
Orgname Username
OP-420-HO-S-00 net1111
RB-430-AD-A-12 net2222
AC-200-RL-B-10 net1234
….
etc.
Orgname=GroupName in AD
I need to read output file with Posh and add username to appropriate GroupName.
How i can do this?
Thanks!
Andriy,
So the input file is basically space-separated list of groupnames and usernames?
Then this should work:
Import-Csv C:\list.csv -Delimiter ‘ ‘ | ForEach-Object {Add-QADGroupMember -Identity $_.Orgname -Member $_.Username }
If you have further questions, please post them to the forums at http://powergui.org – you might get better answers faster than on this blog because there are more people hanging out there. π
Dmitry
It working:)
Thanks for help.
Hi Dmitry,
I attempted the following
Get-Content c:\Temp\corpdl.csv | Add-QADGroupMember “All Company”
The CSV file contains a list of email addresses of about 4000 users. The email addresses are either primary SMTP addresses or part of the proxy address. It throws out an error on the third user because the SMTP address is not primary but part of the contact’s proxy addresses. The list contains email addresses that can be either from a contact or user object. Any ideas how I can get this accomplished or what I’m doing wrong?
Thanks and much appreciated
You can use “Get-QADUser -ProxyAddress” to search by any email addresses. See http://wiki.powergui.org/index.php/Get-QADUser
If you are still lost – try asking at the AD and PowerShell forum at http://powergui.org