Query/List GPOs Linked to Multiple OUs

Hello experts:

I'm trying to put together a PS script to query/list all GPOs that is linked to a list of OUs.

import-module GroupPolicy
$dns = Get-Content c:\temp\list_of_OU_DNs.txt

For Each ($dn in $dns)
(Get-GPInheritance -Target "$dn").GpoLinks | foreach-object {Get-GPO -domain <domain> -Name ($_.DisplayName)} | Select-Object DisplayName,ID | Export-CSV -path c:\temp\results.txt;

The question I have is how to create an output wherein:
the corresponding DN (OU),the associated GPO linked to the OU (DisplayName), GPO GUID (ID)

Can someone please shed some light?

Thank you!
IT_Admin XXXXAsked:
Who is Participating?
With a "calculated property":
$dns | ForEach-Object {
	$dn = $_
	(Get-GPInheritance -Target $dn).GpoLinks | ForEach-Object {
		Get-GPO -domain <domain> -Name ($_.DisplayName) | Select-Object @{n='DN'; e={$DN}}, DisplayName, ID
} | Export-CSV -path c:\temp\results.txt

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.