By using this site, you agree to the Privacy Policy.
Accept
EnterinITEnterinITEnterinIT
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
    • Azure
    • ConfigMgr/SCCM
    • DPM
    • Orchestrator
    • Hyper-V
    • Microsoft Edge Insider
    • MSI
    • Office 365
    • Office Insider
    • Power BI
    • Skype
    • SQL Server
    • Teams
  • WINDOWS
    • Windows Admin Center
    • Windows Client
    • Windows Server
  • SCCM Query Collection List
Reading: PowerShell Active Directory User Group membership
Font ResizerAa
EnterinITEnterinIT
Font ResizerAa
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
  • WINDOWS
  • SCCM Query Collection List
Search
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
    • Azure
    • ConfigMgr/SCCM
    • DPM
    • Orchestrator
    • Hyper-V
    • Microsoft Edge Insider
    • MSI
    • Office 365
    • Office Insider
    • Power BI
    • Skype
    • SQL Server
    • Teams
  • WINDOWS
    • Windows Admin Center
    • Windows Client
    • Windows Server
  • SCCM Query Collection List
IT Pro

PowerShell Active Directory User Group membership

Published: February 11, 2024
2 Min Read
SHARE

PowerShell Active Directory User Group membership. I got the task:

  1. Check if the AD Users are members of AD Group;
  2. If users doesn’t members of this AD Group – add them to the AD Group.

P. S. Probably, it is incorrectly written in English – you may correct me in the comments below.

To complete this task, I wrote the next script:

#create array with users from multiple OU and add filter "user AD object enabled".
$ouArray += Get-ADUser -SearchBase "OU=<your OU name>,OU=<your OU name>,OU=<your OU name>,DC=<your domain name>,DC=<your domain name>,DC=<your domain name>" -Filter {enabled -eq $true} 
$ouArray += Get-ADUser -SearchBase "OU=<your OU name>,OU=<your OU name>,OU=<your OU name>,DC=<your domain name>,DC=<your domain name>,DC=<your domain name>" -Filter {enabled -eq $true}

#declare AD Group for search
$group = "<your group name>"

#declare AD Group for second task - add membership
$group2 = Get-ADGroup "CN=<your group name>,OU=<your OU name>,OU=<your OU name>,DC=<your domain name>,DC=<your domain name>,DC=<your domain name>"

#check membership
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty sAMAccountName
$ouArray | ForEach-Object {
$user = $_.sAMAccountName
If ($members -contains $user)
{
} Else 
{

#if users doesn't exist in AD Group - add them to AD Group

#also you may test this part with next string
#Write-host "$user not exist in group"

Add-ADGroupMember $group2 –Member $user
}
}
TAGGED:Active DirectoryPowerShell
Previous Article How to Delete WiFi Profiles in Windows 10 – Simple Step-by-Step Guide
Next Article SharePoint home in Office 365 updates across Web and mobile
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translation

English中文(简体)DanskNederlandsFrançaisDeutschItaliano한국어PolskiPortuguêsRomânăEspañolУкраїнська
by Transposh - translation plugin for wordpress

Popular Posts

System Requirements for Windows Server 2016
Windows Server
Configuration Manager Query Collection List
Configuration Manager Query Collection List
ConfigMgr
Structured/Managed Navigation enabled on Modern Pages in Classic Team Sites
Tech
SCCM Failed to get DP locations as the expected version from MP
ConfigMgr

Recent Posts

Windows 10 Hyper-V VM Settings
Hyper-V
A Beginner’s Guide to Configuring Hyper-V on Windows 10
Hyper-V
Configuration Manager Query Collection List
SCCM Current Branch Update 1610
ConfigMgr
Configuration Manager Query Collection List
Configuration Manager Client Cache Cleanup Script
ConfigMgr

© 2023 EnterinIT

Go to mobile version
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?