Using PowerShell and a CSV File to Create Multiple Active Directory Groups

Hi All,

There may be times when you need to create many groups in a short amount of time and creating them manually will take too long. In this article I will discuss how I use the “Import-Csv” and the “New-ADGroup” cmdlets along with a csv file to create Active Directory Groups in bulk to save you time. In the example below we will be creating universal security groups. You will need to have the Active Directory Module for PowerShell installed to use the “New-ADGroup” cmdlet. Once you have the Active Directory Module for PowerShell installed you can open PowerShell as Administrator and type the following to import the module (module will be imported automatically when executing the ”New-ADGroup ” cmdlet in PowerShell 3.0).

“Import-Module ActiveDirectory”

To begin you will need to create a csv file containing the names of the groups. In this example I will be creating a csv file named “groups.csv” which contains the names of the 4 groups that will be created. You can do this by opening Notepad and creating a header called “name” and listing the group names line by line all in quotes as shown below. When complete, save the file with a csv extention. In this example I will be saving the file to the “c:\scripts” directory.CreateADGroupsCSV

Now you will to run the script below replacing the following with information from your environment:

Replace ‘c:\scripts\groups.csv’ with the path to the csv file you created earlier. Replace the information following the -Path parameter with the DistinguishedName of the OU where you want the groups to be created in Active Directory. Following the -Description parameter, enter whatever description you feel suitable. After the -GroupScope parameter, choose either DomainLocal, Global or Universal. For the -Managedby parameter, enter the samaccountname for the user that will manage the group if any.

NOTE: The parameters used in the following script and described above are not all required. There are also other parameters you could choose such as -GroupCategory to choose between Distribution and Security groups for example. For more information on the parameters you can simply type “Get-Help New-ADGroup -Full” in powershell to get more information about the cmdlet.

The Script (You can copy and paste the script below in notepad and save it with a ps1 extension or paste and run it in PowerShell ISE):

Import-Module ActiveDirectory
$groups = Import-Csv ‘c:\scripts\groups.csv
foreach ($group in $groups) {
New-ADGroup -Name $group.name -Path “OU=OUWhereIStoreMyGroups,DC=Pipe2Text,DC=com” -Description “New Groups Created in Bulk” -GroupCategory Security -GroupScope Universal -Managedby BC
}

In the script above we created 4 groups from the csv file.  The groups are named Group1, Group2, Group3 and Group4. They are located in the “WhereIStoreMyGroups” OU in the Pipe2text.com domain. The groups are “Universal Security” Groups managed by Active Directory user “BC”.

Well I hope this helps. Remember, its always a good idea to test in a lab scenario first. Also, as mentioned earlier, for more information about the “New-ADGoup” cmdlet, you can simply type “Get-Help New-ADGroup -Full” in powershell to get more info about the cmdlet. If you have any questions or feedback, please leave a comment.

Related Links:

Using PowerShell and Active Directory to Create a Server or Workstation Inventory

Using PowerShell to find Stale Computers in Active Directory

Moving Stale Computers in Active Directory to an OU using PowerShell

Using PowerShell and a Text File to Delete Multiple Active Directory Groups

Using PowerShell to export Active Directory Group Members to a CVS File

Leave a Reply

Your email address will not be published. Required fields are marked *