Tuesday, July 16, 2024

Quick and Easy User Addition to AD Groups with PowerShell

Hello, fellow IT enthusiasts!

Today, I want to share a nifty PowerShell script that I often use in my day-to-day tasks as a system administrator. If you've ever needed to add a single user to multiple Active Directory (AD) groups, you'll appreciate the simplicity and efficiency of this one-liner.

The Command:

$groups = "ADGroup1","ADGroup2","ADGroup3","ADGroup4"; $user = "<user>"; $groups | ForEach-Object { Add-ADGroupMember -Identity $_ -Members $user } 

 Explanation:

This command lets you add a specified user to multiple AD groups in a single line. Let's break it down:

  1. $groups = "ADGroup1","ADGroup2","ADGroup3","ADGroup4":

    • Here, we define an array of AD group names. Replace "ADGroup1", "ADGroup2", etc., with the actual group names you want to use.
  2. $user = "<user>":

    • This is where you specify the username of the user you want to add. Replace "<user>" with the actual username.
  3. $groups | ForEach-Object { Add-ADGroupMember -Identity $_ -Members $user }:

    • This pipeline takes each group in the $groups array and applies the Add-ADGroupMember cmdlet to add the user to each group.

Real-World Use Case:

In my role at Soprema , I often need to quickly add users to several AD groups to ensure they have the right permissions and access. This script saves me a lot of time and minimizes the risk of manual errors.

Feel free to tweak the script according to your needs and let me know how it works for you. Happy scripting!

No comments:

Post a Comment

[Coding with python][Installing Flask App]

  I am trying website coding with Python to better organise my tools. For starters, I installed Flask. Below are the commands that I am usin...