John.Doe Jane.Doe Jane.SmithThen you will need to run this in the PowerShell ISE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Import -Module ActiveDirectory # Each row of the text file will be consider one object. The object being the Active Directory identity (SamAccountName). $names = Get-Content c:\names.txt # Set this to $false when you are ready to make the changes. $whatIf = $true foreach ( $name in $names ) { $user = Get -ADUser -Filter { SamAccountName -eq $name } -Properties MemberOf if( $user ) { # The groups object will contain a list of Active Directory groups by their distinguished name. # (i.e. CN=GroupName_A-C,OU=Groups,OU=Company,DC=pri,DC=company,DC=com) $groups = $user | Select-Object -ExpandProperty MemberOf if( -not ( $groups -like 'CN=GroupName_*' )) { $groupName = '' switch -Regex ( $user .SamAccountName[0]) { # Match the first letter as a, b, or c. "[a-c]" { $groupName = 'GroupName_A-C' } # Match the first letter as d, e, f, or g. "[d-g]" { $groupName = 'GroupName_D-G' } "[h-k]" { $groupName = 'GroupName_H-K' } "[l-q]" { $groupName = 'GroupName_L-Q' } "[r-t]" { $groupName = 'GroupName_R-T' } "[u-z]" { $groupName = 'GroupName_U-Z' } } if( $groupName ) { "Adding $($user.SamAccountName) to the group $($groupName)" Add -ADGroupMember -Identity $groupName -Members $user .SamAccountName -WhatIf : $whatIf } } else { "Skipping $($user.SamAccountName) because they are already in the group $($groupName)" } } else { "$($name) does not exist" } } |
No comments:
Post a Comment