Prerequisites
If you’d like to follow along with this tutorial, be sure you have the following prerequisites in place:- A Windows computer (Windows 10/11 or Windows Server) joined to an Active Directory domain
- The PowerShell Active Directory module installed
- A user account with permissions to manage AD groups
Querying AD Groups with PowerShell
Let’s start with a common scenario – you’re the new IT admin at a company and need to audit the AD group structure. Your manager wants to know what groups exist across different departments. TheGet-ADGroup
cmdlet will be your best friend here.
Finding Groups by Name
Perhaps the simplest task is finding groups containing specific text in their name. For example, to find all groups with “Sales” in the name:Get-ADGroup -Filter 'Name -like "Sales"'
Filtering by Group Type
Maybe you only want to see security groups (not distribution groups). You can add additional filter criteria using the-and
operator:
Get-ADGroup -Filter 'Name -like "Sales" -and GroupCategory -eq "Security"'
Searching in Specific OUs
Need to find groups in a particular organizational unit (OU)? Use theSearchBase
parameter:
Get-ADGroup -Filter * -SearchBase 'OU=Engineering,DC=company,DC=local'
Finding Recently Created Groups
Want to see which groups were created after a certain date? Filter on thewhenCreated
attribute:
Get-ADGroup -Filter 'whenCreated -ge "2023-01-01"'
Creating New AD Groups
Now let’s look at creating new groups. Maybe your company is restructuring and you need to create groups for new departments.Creating a Security Group
Here’s how to create a new security group for IT support staff:New-ADGroup -Name "IT_Support" `
-GroupScope Global `
-GroupCategory Security `
-Description "Group for IT support staff" `
-Path "OU=IT,DC=company,DC=local"
Creating a Distribution Group
Need an email distribution group? Just change a few parameters:New-ADGroup -Name "Marketing_News" `
-GroupScope DomainLocal `
-GroupCategory Distribution `
-Description "Group for receiving marketing updates" `
-Path "OU=Marketing,DC=company,DC=local"
Creating Multiple Groups at Once
Got multiple similar groups to create? Use a loop:$regions = "North", "South", "East", "West"
foreach ($region in $regions) {
New-ADGroup -Name "Sales_$region" `
-GroupScope Global `
-GroupCategory Security `
-Description "Sales team for $region region" `
-Path "OU=Sales,DC=company,DC=local"
}
Modifying Existing Groups
Things change in organizations. Groups need to be renamed, descriptions updated, and scopes modified. Let’s see how to handle these tasks.Renaming Groups
To rename a group, you’ll need to change both its name and samAccountName:# First rename the group object
Get-ADGroup EngineeringTeam | Rename-ADObject -NewName TechTeam
# Then update the samAccountName
Get-ADGroup EngineeringTeam | Set-ADGroup -SamAccountName TechTeam
Updating Group Descriptions
Need to update a group’s description? One line withSet-ADGroup
:
Get-ADGroup TechTeam | Set-ADGroup -Description 'Technical Team for Engineering Projects'
Changing Group Scope
If you need to change a group’s scope (like from Global to Universal):Get-ADGroup TechTeam | Set-ADGroup -GroupScope Universal
Pro Tips
Here are some tips to make your AD group management even more efficient:-
- Always use `-Filter` instead of `-Identity` when querying multiple groups – it’s more efficient
- Remember that group scope can’t be changed if the group has members – remove members first
- Use the `-WhatIf` parameter when making changes to preview what would happen
- Always test your group changes in a non-production environment first
Summary
You should now have a solid foundation for managing AD groups with PowerShell. While the Active Directory Users and Computers snap-in is great for one-off tasks, PowerShell gives you the power to automate and manage groups at scale.Remember – the examples shown here are just the beginning. PowerShell’s AD cmdlets are incredibly powerful and flexible. As you become more comfortable with these basics, you can build more complex solutions to match your organization’s needs. Need to learn more about Active Directory PowerShell? Check out our other tutorials:Today’s sponsor is ScriptRunner, your #1 platform to accelerate your IT automation with PowerShell. They offer a FREE PDF cheat sheet, designed to be your go-to guide for the most important and frequently used Active Directory cmdlets. Download for free