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:
$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.
- Here, we define an array of AD group names. Replace
$user = "<user>":
- This is where you specify the username of the user you want to add. Replace
"<user>"
with the actual username.
- This is where you specify the username of the user you want to add. Replace
$groups | ForEach-Object { Add-ADGroupMember -Identity $_ -Members $user }:
- This pipeline takes each group in the
$groups
array and applies theAdd-ADGroupMember
cmdlet to add the user to each group.
- This pipeline takes each group in the
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