Powershell - List AD Organizational Unit and GPOs linked to them

The below script will search Active Directory for all Organizational Unit which contain specific name and list them along with all Group Policies linked to those OUs.

Example: The below command will get a list of all OUs which name contains 'Test'. It will also show the details of all GPOs linked to OUs.

Usage Example:
.\Get-OUList.ps1 –OUName “Test”

#Script


[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True)]
  [string]$OUName

  )


$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$outputfile = $directorypath + "\Result.csv"
$OUName = "*" + $OUName + "*"
$Results = @()

$OUList=Get-ADOrganizationalUnit -Filter * | Where-Object -FilterScript {$PSItem.distinguishedname -like $OUName}

foreach($OU in $OUList){
    $LinkedGPOs = Get-ADOrganizationalUnit -Identity $OU | select -ExpandProperty LinkedGroupPolicyObjects           
           
    foreach($LinkedGPO in $LinkedGPOs) {            
        $GPO = [adsi]"LDAP://$LinkedGPO" | select *  
       
        $properties = @{
        OUName=$OU.DistinguishedName
        GPOName=$GPO.displayName.Value
        GPOGUID=$GPO.Guid
        GPOWhenCreated=$gpo.whenChanged.Value
        GPOWhenChanged = $gpo.whenChanged.Value
       
        }
    
        $Results += New-Object psobject -Property $properties
     }
}       
           
$Results | Select-Object OUName,GPOName,GPOGUID,GPOWhenCreated,GPOWhenChanged | Export-Csv -notypeinformation -Path $outputfile


Comments

  1. Thank you very much for this script, had spent days for such script. This script helped me lot, appreciate your efforts!! Keep up the good work :)

    ReplyDelete
  2. Is there a way to automate all Ous as variable in the script or I can just feed * ?

    ReplyDelete

Post a Comment

Popular posts from this blog

SCCM Software Distribution Troubleshooting

Troubleshooting System Center Endpoint Protection (SCEP) Client