Link List of GPOs to List of OUs

  • 30 November 2016
  • Sean Huggans
Description: 

Wrote this while needing to list the same set of GPOs to many OUs.

Language: 
PowerShell
Usage Type: 
Standalone
Script Source: 
#########################################################
#
# Name: Link-GPOstoSubOUsBasedOnName.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"
$LinkToAllSubOUs = $false #Change to $true to link to sub OUs of found OUs Named
$SkipOUKeyword1 = "filter1"
$SkipOUKeyword2 = "filter2"
$SkipOUKeyword3 = "filter3"
 
######################
[array]$GPOList = $(Get-Content $GPOList)
 
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
}
 
[array]$OUFilter = Get-ADOrganizationalUnit -Filter "Name -eq '$OUsNamed'"
foreach ($ComputerOU in $OUFilter | Sort) {
    if ($LinkToAllSubOUs -eq $true) {
        $SubOUs = Get-ADOrganizationalUnit -SearchBase $ComputerOU  -SearchScope Subtree -Filter * | Select-Object DistinguishedName
        foreach ($SubOU in $SubOUs) {
            if (($SubOU.DistinguishedName -notlike "*$($SkipOUKeyword1)*") -and ($SubOU.DistinguishedName -notlike "*$($SkipOUKeyword2)*") -and ($SubOU.DistinguishedName -notlike "*$($SkipOUKeyword3)*"))  {
                write-host “———————————————-----------------------------—”
                write-host $SubOU.DistinguishedName
                write-host “———————————————-----------------------------—”
                ForEach ($title in $GPOList) {
                    write-host $title
                    LinkGPO -title $title -ListedOU $SubOU.DistinguishedName
                }
                write-host "---"
                write-host ” ”
            } else {
                write-host “———————————————-----------------------------—”
                write-host $SubOU.DistinguishedName
                write-host “———————————————-----------------------------—”
                write-host "No GPOs were linked due to filtering."
                write-host "---"
                write-host ” ”
            }
        }
    } else {
        if (($ComputerOU.DistinguishedName -notlike "*$($SkipOUKeyword1)*") -and ($SubOU.DistinguishedName -notlike "*$($SkipOUKeyword2)*") -and ($SubOU.DistinguishedName -notlike "*$($SkipOUKeyword3)*"))  {
            write-host “———————————————-----------------------------—”
            write-host $ComputerOU.DistinguishedName
            write-host “———————————————-----------------------------—”
            ForEach ($title in $GPOList) {
                write-host $title
                LinkGPO -title $title -ListedOU $ComputerOU.DistinguishedName
            }
            write-host "---"
            write-host ” ”
        } else {
            write-host “———————————————-----------------------------—”
            write-host $ComputerOU.DistinguishedName
            write-host “———————————————-----------------------------—”
            write-host "No GPOs were linked due to filtering."
            write-host "---"
            write-host ” ”
        }
    }
}
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.