This PowerShell script searches Active Directory for all Organizational Units (OUs) whose names contain a specified keyword and lists each matching OU along with the Group Policy Objects (GPOs) linked to it.
Example: The command below searches for all OUs with names containing “Test” and displays the details of every GPO linked to those 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
Related Posts:
- Powershell – Get System up time
- Powershell Script : List AD Organizational Unit and GPOs linked to them
- Powershell – Compare hardware and software details on two computers
- Powershell Script : Retrieve AD Computers Properties
- Powershell – Merge CSV files & Insert file name as a column
- Powershell Script: Validate if Computer account exists in Active Directory
- Powershell remote – WinRM cannot complete the operation
- Powershell script to Add bulk users / computers to AD Group
- PowerShell Script : Copy AD Group Membership
- Using PowerShell Behind a Proxy Server