Monday, July 25, 2016

Powershell - Mass Link a Group Policy Object to a list of OUs

Powershell - Mass Link a Group Policy Object to a list of OUs

Step 1: Get your GP Object and save it to a variable:

$SystemCenter = Get-GPO -Name "System Center"

Step 2: Get your List of OUs

For me, my OUs are stored under Domain Users. This is a terrible practice, but it was implemented long before me, and moving it was going to be far more trouble than what the effort would yield. 
$OUs = Get-ADOrganizationalUnit -filter * -SearchBase "OU=Domain Users,DC=bbox,DC=org" | ? {$_.Name -like "*Computer*"}

Step 3: Link 'em Up


foreach ($obj in $OUs){
    #new-GPLink -Target $obj  -Guid $SystemCenter.Id --Whatif
    new-GPLink -Target $obj  -Guid $SystemCenter.Id
}

I recommend doing that with the What-IF parameter first. (commented out) to make sure you don't mess anything up.
Toy around with the Filter and Where-Object pipes from step 2 until you have the list that you like. You can alternatively just create a single dimensional array with the Distinguished Name of the OU as the only element. 

No comments:

Post a Comment