Share this post!

In the previous example, which you can find here, I shared how to very quicky create 77 Active Directory test users based on the names of United States presidents and vice presidents. See the post: Creating Active Directory Test User Accounts with PowerShell

In this post, I’ll build upon the test environment previously created, showing how to very quickly create 55 Active Directory Security Groups based on common Corporate department names. Then, I’ll take the 77 Active Directory test users previously created, and populate each of the groups with a random selection of 15 members per group. You can take this code sample and adjust the number of members per group, using the global variables at the top of the script. When it’s done, you’ll have a list of 55 departments, each with 15 randomly selected members from the list of presidents and vice presidents previously created.  Check it out and enjoy!

Because I’m lazy and dislike writing inline documentation in my PowerShell scripts, I’ve kindly and very politely asked Chat GPT to build the documentation lines for me. Nicely done, Chat GPT. I’ll keep you around for another go.

# Set the variables that will be used throughout the script
$groupnameprefix = "RESOURCE_DEPT_"
$organizationalunitpath = "ou=Politicians,ou=users,ou=company,dc=mydomain,dc=com"
$grouporganizationalunitpath = "ou=Departments,ou=groups,ou=company,dc=mydomain,dc=com"
$NumUsersPerGroup = "15"
# This is a list of department names that will be used to generate group names
$departments = @(
"Accounting and Finance",
"Business Development",
"Compliance and Risk Management",
"Corporate Social Responsibility",
"Customer Service",
"Data Analytics",
"Facilities Management",
"Human Resources",
"Information Technology",
"Logistics and Supply Chain Management",
"Product Development",
"Project Management",
"Purchasing and Procurement",
"Quality Assurance and Control",
"Research and Development",
"Strategic Planning",
"Talent Management",
"Training and Development",
"Business Analysis",
"Corporate Communications",
"Creative Services",
"Customer Experience",
"Digital Marketing",
"Event Planning and Management",
"Graphic Design",
"Information Management",
"Intellectual Property",
"International Business",
"Inventory Control",
"Market Research",
"Media Relations",
"Packaging and Labeling",
"Pricing and Revenue Management",
"Product Management",
"Public Relations",
"Regulatory Affairs",
"Sales Operations",
"Social Media Management",
"Store Operations",
"Vendor Management"

# Get all users from the specified organizational unit
$users = Get-ADUser -Filter * -SearchBase $organizationalunitpath

# Create the new group
foreach ($department in $departments){
# Generate the group name by concatenating the prefix and the department name with spaces removed
$groupname = $groupnameprefix + $department.Replace(" ","")
# Create the group using the generated name and the specified organizational unit path
New-ADGroup -Name $groupName -path $grouporganizationalunitpath -GroupScope Global

# Randomly select the specified number of users and add them to the group
$randomUsers = $users | Get-Random -Count $NumUsersPerGroup
foreach ($user in $randomUsers) {
Add-ADGroupMember -Identity $groupName -Members $user

Share this post!