Link List of GPOs to Sub OUs Based On OU Name

  • 1 December 2016
  • Sean Huggans
Description: 

The following script is useful in cases where you need to link the same set of GPOs (provided in a list) to all sub OUs of OUs matching a given keyword (for example, "Controlled Devices").

Language: 
PowerShell
Usage Type: 
Standalone
Script Source: 
#########################################################
#
# Name: LinkGPOstoSubOUsBasedOnName.ps1
# Author: Sean Huggans
# Version: 1.0
# Date: 2016.11.30
# Links GPOs provided in a supplied list to OUs provided
# in a supplied list.
#
#########################################################
Import-Module ActiveDirectory
Import-Module GroupPolicy
 
$GPOList = "c:\temp\gpolist.txt"
$OUsNamed = "Computers"
 
function LinkGPO ($title, $ListedOU) {
# Create the link on the target
New-GPLink -Name $title -Target $ListedOU -LinkEnabled "Yes" -confirm:$false
# Set the link order on the target
Set-GPLink -Name $title -Target $ListedOU -Order 1 -confirm:$false
# Set the original enforcement setting on the target
Set-GPLink -Name $title -Target $ListedOU -Enforced "No" -confirm:$false
}
 
$OUFilter = Get-ADOrganizationalUnit -Filter "Name -eq '$OUsNamed'"
foreach ($ComputerOU in $OUFilter | Sort) {
 $SubOUs = Get-ADOrganizationalUnit -SearchBase $ComputerOU  -SearchScope Subtree -Filter * | Select-Object DistinguishedName
 foreach ($SubOU in $SubOUs) {
  if (($SubOU.DistinguishedName -notlike "*filter1*") -and ($SubOU.DistinguishedName -notlike "*filter2*") -and ($SubOU.DistinguishedName -notlike "*filter3*"))  {
   echo “———————————————-----------------------------—”
   echo $SubOU.DistinguishedName
   echo “———————————————-----------------------------—”
   ForEach ($title in $(Get-Content $GPOList)) {
    echo $title
    LinkGPO -title $title -ListedOU $SubOU.DistinguishedName
      }
   echo "---"
   echo ” ”
  } else {
   echo “———————————————-----------------------------—”
   echo $SubOU.DistinguishedName
   echo “———————————————-----------------------------—”
   echo "No GPOs were linked due to filtering."
   echo "---"
   echo ” ”
  }
 }
}
Note: that all applications posted here are posted for use, both commercial and non-commercial, free of charge, and as such are provided as-is, without warranty of any kind whatsoever. visuaFUSION, FMSCUG or any other program listed here's author are not responsible for any damages or shortcomings that result from usage of any of these applications.
Error | visuaFUSION Systems Solutions Blog

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /mnt/home/visuafus/public_html/bahusa.net/includes/common.inc:2861) in drupal_send_headers() (line 1551 of /mnt/home/visuafus/public_html/bahusa.net/includes/bootstrap.inc).
  • Error: Call to undefined function mail() in DefaultMailSystem->mail() (line 79 of /mnt/home/visuafus/public_html/bahusa.net/modules/system/system.mail.inc).

Error

The website encountered an unexpected error. Please try again later.