Search code examples
vbscriptgpo

How to find the GUID of a GPO given it's name?


Does anyone know of a way to find the GUID of a specific GPO given its name using VBScript? I've seen a lot of examples to go from a GUID to a GPO, but not the other way around.


Solution

  • Use an LDAP query that filters for a given display name. The name attribute of the GPO contains the GUID.

    displayName = "..."
    
    domain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")
    
    Set cn = CreateObject("ADODB.Connection")
    cn.Provider = "ADsDSOObject"
    cn.Open "Active Directory Provider"
    
    Set cmd = CreateObject("ADODB.Command")
    Set cmd.ActiveConnection = cn
    cmd.CommandText = "SELECT name " & _
                      "FROM 'LDAP://CN=Policies,CN=System," & domain & "' " & _
                      "WHERE objectClass = 'groupPolicyContainer' AND " & _
                      "displayName = '" & displayName & "'"
    
    Set rs = cmd.Execute
    Do Until rs.EOF
      WScript.Echo rs.Fields("name").Value
      rs.MoveNext
    Loop