Last active
May 18, 2023 20:56
-
-
Save jessewolcott/989b0e8681c3d080f2019b023486d16b to your computer and use it in GitHub Desktop.
OU to Group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Full_OU_Path = "" # Example: Full OU path. Right click in ADUC and go to object! "contoso.com/Systems/Windows10" | |
$NewGroup = "" # Example: Group name, displayname should work, but SAM would probably be preferrable. "New York Office" | |
Write-Output "Getting OU Path from Canonical name" | |
$OU_Path = (Get-ADOrganizationalUnit -Filter * -Properties CanonicalName,Name,DistinguishedName | Where-Object -FilterScript {$_.CanonicalName -like "*$Full_OU_Path"} | Select-Object -ExpandProperty DistinguishedName) | |
Write-Output "Found $OU_Path" | |
Write-Output "Getting Users from $NewGroup" | |
$Users = Get-ADUser -Filter * -SearchBase $OU_Path | |
Write-Output ("Found "+($users.count)+" Users") | |
$Group = Get-ADGroup -Filter {Name -eq $NewGroup} | |
foreach ($User in $Users){ | |
Write-Output ("Found "+$User.Name) | |
$Group | Add-ADGroupmember -Members $User -Verbose -whatif | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment