Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Created January 18, 2021 16:47
Show Gist options
  • Select an option

  • Save jdhitsolutions/f33ec03729c019efacc738a762d9a725 to your computer and use it in GitHub Desktop.

Select an option

Save jdhitsolutions/f33ec03729c019efacc738a762d9a725 to your computer and use it in GitHub Desktop.
A PowerShell function to list Group Policy links
@jdhitsolutions

jdhitsolutions commented Jan 18, 2021

Copy link
Copy Markdown
Author

These files are described at https://jdhitsolutions.com/blog/powershell/8041/get-group-policy-links-with-powershell/.
This script will not work properly in PowerShell 7. It must be run from Windows PowerShell 5.1.

@samredinet

samredinet commented May 10, 2023

Copy link
Copy Markdown

Is this still maintained? Tried to use this today and the following returned

Method invocation failed because [System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]]] does not contain a method named 'new'.
At line:280 char:9
+         $targets = [System.Collections.Generic.list[string]]::new()
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

@jdhitsolutions

Copy link
Copy Markdown
Author

This function still works for me without error in Windows PowerShell 5.1. What version of PowerShell are you using, and on what OS?

@samredinet

samredinet commented May 10, 2023 via email

Copy link
Copy Markdown

@jdhitsolutions

Copy link
Copy Markdown
Author

That doesn't help me much. Can you run this line by itself?

$targets = [System.Collections.Generic.list[string]]::new()

@jimbobmcgee

Copy link
Copy Markdown

This is nice. There aren't enough examples of PS formatting in the world, and $PSDefaultParameterValues is an interesting solution to the passthrough of the AD-targeting parameters.

I rolled my own, as I needed something a little more paste-able (rather than a module). Far fewer features than yours, but handles getting from sites and OUs without needing a COM.

I leave it below for no reason other than my own long-term memory...

# Find all GPO links in the domain and its subtree...
$script:gplinks = Get-ADDomain `
                | ForEach-Object { ($_.DistinguishedName,$_.SubordinateReferences) | %{$_} } `
                | ForEach-Object { Get-ADObject -LDAPFilter '(gPLink=*)' -SearchBase $_ -SearchScope Subtree -Properties gPLink } `
                | ForEach-Object {
                    $o = $_
                    ($_.gPLink | Select-String '\[LDAP://cn={([0-9A-F-]+)},.*?;(\d)\]' -AllMatches).Matches `
                               | Select-Object @{n='GpoId';e={$_.Groups[1].Value}},@{n='LinkedTo';e={$o}},@{n='State';e={$_.Groups[2].Value}}
                  } `
                | Sort-Object GpoId
       
# List all GPOs with their links...
Get-GPO -All | Select-Object *,@{n='LinkedTo';e={ $g = $_; ($gplinks | Where-Object { $_.GpoId -ieq $g.Id }).LinkedTo }}
       
# Find all GPOs not currently linked...
Get-GPO -All | Where-Object { $gplinks.GpoId -inotcontains $_.Id }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment