Need help with powershell ADSI query that loops to find members of an AD group

JP Luna 21 Reputation points

I have a PowerShell script below where I'm able to get the group members of an AD group. It returns the columns name, SchemaClassName and UserAccountControl. What I'm trying to do now is I want to add a switch/if statement that replaces the UserAccountControl results from bit to either "True" or "False" string. I have tried multiple iterations and have been unsuccessful at it. I am using ADSI because the environment that I am using it for does not allow the get commands. If anyone could point me in the right direction would be greatly appreciated!

Good code
$Group = [ADSI]"LDAP://cn=Groupname,OU=Groups,OU=Users,OU=xxxx,dc=xxx,dc=xxx,dc=xxx"
$Members = $Group.Member | ForEach-Object {[ADSI]"LDAP://$"}
$Members | Select-Object | Select @{N='Members' ;e={$
.name}}, @{N='Type' ;e={$.SchemaClassName}}, @{N='Enabled' ;e={$.UserAccountControl}}

Broken code
$Group = [ADSI]"LDAP://cn=Groupname,OU=Groups,OU=Users,OU=xxxx,dc=xxx,dc=xxx,dc=xxx"
$Members = $Group.Member | ForEach-Object {[ADSI]"LDAP://$"}
$Members | Select-Object | Select @{N='Members' ;e={$
.name}}, @{N='Type' ;e={$.SchemaClassName}}, @{N='Enabled' ;e={$.UserAccountControl}} $Enabled -eq '512' if ($Enabled -eq "512"){"True"} elseif ($Enabled -eq 514){"False"} elseif ($Enabled -eq 66048){"True"} elseif ($Enabled -eq 66050){"False"} else {""}

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,405 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points

    I forgot . . . it's been so long since I've used ADSI. There's a missing subscript in your script! See the last calculated property in the last Select-Object.

    $Group = [ADSI]"LDAP://cn=Groupname,OU=Groups,OU=Users,OU=xxxx,dc=xxx,dc=xxx,dc=xxx"
    $Members = $Group.Member | 
        ForEach-Object {[ADSI]"LDAP://$_"}
    $Members |  
        Select-Object @{N='Members' ;e={$}}, @{N='Type' ;e={$_.SchemaClassName}}, @{N='Enabled' ;e={($_.UserAccountControl[0] -band 2) -eq 2}}

    The result is:

    Members              Type Enabled
    -------              ---- -------
    Melvin McPhucknuckle user    True
    XXX                  user   False

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points

    I think this is what you were aiming for, but it's difficult to know because the "Select" line is pretty wonky!

    # 512   = normal account
    # 514   = normal account, account disabled?
    # 66048 = normal account, don't expire password 
    # 66050 = normal account, account disabled, don't expire password
    Get-ADGroupMember -Identity Groupname |
            $u = Get-ADUser -Identity $_.distinguishedname
                Members         = $u.Name
                Type            = $u.objectclass
                Enabled         = $u.Enabled
                PasswordExpires = $u.PasswordNeverExpires

    When you post code, please use the "Code Sample" editor (it's the 5th icon from the left on the Format Bar (the icon has the graphic "101 010").

    If you post code using the "normal" editor the code gets mangled (notice in your post that the underbar characters disappeared!). It also makes separating code from commentary difficult.

  2. JP Luna 21 Reputation points

    @Rich Matheisen
    Here's the script that I have currently.

            Function DecodeUserAccountControl ([int]$UAC)  
            $UACPropertyFlags = @(  
            return (0..($UACPropertyFlags.Length) | ?{$UAC -bAnd [math]::Pow(2,$_)} | %{$UACPropertyFlags[$_]}) -join ” | ”  
            $Group = [ADSI]"LDAP://cn=xxx,OU=xxx,OU=xxx,OU=xxx,dc=xxx,dc=xxx,dc=xxx"  
            $Members = $Group.Member | ForEach-Object {[ADSI]"LDAP://$_"}  
            $Members | Select-Object | Select @{N='Members' ;e={$}}, @{N='Type' ;e={$_.SchemaClassName}},@{N='Enabled' ;e={DecodeUserAccountControl($_.userAccountControl)}}   
    0 comments No comments

  3. Rich Matheisen 45,111 Reputation points

    In order to get the useraccountcontrol property the script must be run "as administrator".

    Runing the script simple logged in as an administrator account isn't enough. You must run the script in an elevated session.

    0 comments No comments