Skip to content

Tech & Cyber Security

Technology, Cybersecurity, Software Development and Personal Blog!

Primary Menu

  • Home
  • Latest Posts
  • Resources
  • About
  • Contact

Tag AD Account Management

Adding users to AD Security Groups with PowerShell

December 2, 2022December 2, 2022Glenn Mitchell Leave a comment

Adding a user to many security groups in Active Directory using PowerShell is simple. You can use the Add-ADPrincipalGroupMembership cmdlet. This cmdlet allows you to add a user to one or more security groups.

Here’s an example of how you might use this cmdlet (this is an OK solution):

# Define the user you want to add to the security groups
$user = Get-ADUser -Identity "Jeffrey"

# Define the security groups you want to add the user to
$groups = @("DelegatedDomainJoiner", "App1_RO", "App2_Admin")

# We now use a foreach loop to add the user to each group
foreach ($group in $groups)
{
    Add-ADPrincipalGroupMembership -Identity $user -MemberOf $group
}

The Add-ADPrincipalGroupMembership cmdlet, is used inside a foreach loop. The user stored in the $user variable to each security group stored in the $groups array. But this method requires you to loop through all the groups and add the user to each one. This is OK, but we can do it a bit better from code readability standpoint.

We can use the -Members parameter of the Add-ADGroupMember cmdlet instead. To add the user to many security groups at once, take a look at the example below (this is a BETTER solution):

# Define the user you want to add to the security groups
$user = Get-ADUser -Identity "Jeffrey"

# Define the security groups you want to add the user to
$groups = @("DelegatedDomainJoiner", "App1_RO", "App2_Admin")

# This command will add the user to all the groups at once
Add-ADGroupMember -Identity $groups -Members $user

Here, we are using the Add-ADGroupMember cmdlet. The user stored in the $user variable to all the security in a single command. I find this method to be much more concise.

Note: Of course, you will need the correct permissions in Active Directory and the Active Directory PowerShell module.

  • Twitter
  • LinkedIn

Recent Posts

  • Managing a Data Breach: Insights from FinTechSec Solutions Inc.’s Experience January 11, 2024
  • The Essential Guide to Role-Based Access Control December 10, 2022
  • Should I use Chocolatey or Winget? December 4, 2022
  • Adding users to AD Security Groups with PowerShell December 2, 2022
  • Getting security group memberships for the currently logged in user – with PowerShell August 10, 2021

Categories

  • Certifications
  • Fluff
  • Information Security
  • PowerShell and Scripting
  • Software
  • Tips

Tags

Access Control AD Account Management advice Authentication Authorisation automation Certification Exam chocolatey CIRP CISSP cyber-security cybersecurity data breach framework InfoSec Kerberos onboarding package manager PowerShell RBAC regulations Role-Based Access Control Roles security Security Group Study technology whoami winget

I am always happy to connect with you on LinkedIn or Twitter!

Follow me on Twitter

My Tweets

Disclaimer

All opinions are my own and do not reflect those of my employer. Any advice given is to be assessed to ensure that it is suitable for you, your organisation and / or situation.

Hi, I’m Glenn!

Glenn's Profile Picture
Website Powered by WordPress.com.
  • Subscribe Subscribed
    • Tech & Cyber Security
    • Already have a WordPress.com account? Log in now.
    • Tech & Cyber Security
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar