PowerShell not displaying AD Group membership properly

Hi forum,

I am developing one PowerShell script I need your help here to complete it. This PowerShell script will display a GUI but it not showing the Active Directory group membership properly

#Creating Window Form

[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[Windows.Forms.Application]::EnableVisualStyles()
Import-Module ActiveDirectory

$mainForm = New-Object System.Windows.Forms.Form
$mainform.StartPosition = "centerparent " 
$Menuform = New-Object System.Windows.Forms.MenuStrip
$mainForm.Text = "Active Directory Tools v1.0"
$mainform.Size = New-Object System.Drawing.Size(600,200)

#MenuItems Portion
$mainmenu = New-Object System.Windows.Forms.ToolStrip 
$menufile = New-Object System.Windows.Forms.ToolStripMenuItem
$adduser = New-Object System.Windows.Forms.ToolStripMenuItem
$deluser = New-Object System.Windows.Forms.ToolStripMenuItem
$Searchuser = New-Object System.Windows.Forms.ToolStripMenuItem
$menuabout = New-Object System.Windows.Forms.ToolStripMenuItem
$Searchmenu= New-Object System.Windows.Forms.ToolStripMenuItem
$Exitmenu = New-Object System.Windows.Forms.ToolStripMenuItem

# Creating File Menu

$menufile.Text = "&File"
$Menuform.Items.Add($menufile)
$deluser.Text = "&Delete user"
$Menuform.Items.Add($deluser)
$searchuser.Text = "&Search User"
$Menuform.Items.Add($searchuser)

#File/add

$adduser.Text = "&Add User"
$menufile.DropDownItems.Add($adduser)
$adduser.add_click({add})

#File /add /delete

$deluser.Text = "&Delete User"
$menufile.DropDownItems.Add($deluser)
$deluser.add_click({Remove})



#File /add /delete/Search user

$searchuser.Text = "&Search User"
$menufile.DropDownItems.Add($searchuser)
$searchuser.add_click({search})

#Exit

$Exitmenu.Text = "&Exit"
$Exitmenu.add_click({$mainForm.Close()})
[void]$menuFile.DropDownItems.Add($Exitmenu)



#SearchMenu

$Searchmenu.Text = "&Search"
$Menuform.Items.Add($Searchmenu)
$Searchmenu.Add_click({Search})

#About
$menuabout.Text = "&About"
$Menuform.Items.Add($menuabout)
$menuabout.Add_click({About})



#to display the Menubar item
$mainForm.Controls.Add($mainmenu)
$mainForm.Controls.Add($Menuform)

#Function About
Function About {


$Aboutform = New-Object System.Windows.Forms.Form
$Aboutform.StartPosition = "centerparent " 

$Aboutform.Size = New-Object System.Drawing.Size(650,250)
$Aboutform.Text = "About v 1.0"

# About Name Label
    $aboutFormNameLabel = New-Object System.Windows.Forms.Label
    $aboutFormNameLabel.Font     = New-Object Drawing.Font("Tahoma", 10, [System.Drawing.FontStyle]::Bold)
    $aboutFormNameLabel.Location = New-Object System.Drawing.Size(130,10)
    $aboutFormNameLabel.Size     = New-Object  System.Drawing.Size(650,50)
    $aboutFormNameLabel.Text     = "Active Directory Transformation Team"
    $aboutForm.Controls.Add($aboutFormNameLabel)

    
    $aboutformtext = New-Object System.Windows.Forms.Label

    $aboutformtext.size = New-Object System.Drawing.Size(200,40)
    $aboutformtext.Location = New-Object System.Drawing.Size(170,35)
    #$Aboutformtext.Text = "Wintel Support Level II DMT and INT `r `n marlon.ocillos@hpe.com"
    $Aboutform.Controls.Add($Aboutformtext)

    # About Exit Button
    $aboutformexit = New-Object System.Windows.Forms.Button
    $aboutFormExit.Location = new-object System.Drawing.Size(150,75)
    $aboutformexit.Size= New-Object System.Drawing.Size(150,50)
    $aboutFormExit.Text     = "&Ok"
    $aboutForm.Controls.Add($aboutFormExit)
    $aboutformexit.Add_Click({$Aboutform.Close()})
        #Comment Buton

    $aboutcomment = New-Object System.Windows.Forms.Button
    $aboutcomment.Location = new-object System.Drawing.Size(350,75)
    $aboutcomment.Text     = "&Comment"
    $aboutcomment.Size = New-Object System.Drawing.Size(150,50)
    $aboutForm.Controls.Add($aboutcomment)
    $aboutcomment.Add_Click({comment})

    $aboutform.ShowDialog()
    }

#function Search User
Function Search{

$Searchform = New-Object System.Windows.Forms.Form
$Searchform.StartPosition = "centerparent " 
$Searchform.Size = New-Object System.Drawing.Size(600,250)
$searchform.Text = "Search Actie Directory User Account"

# Seacrh Name Label
$Searchformlabel = New-Object System.Windows.Forms.Label
$Searchformlabel.Text = "Enter UserName"
$Searchformlabel.Location = New-Object System.Drawing.Size(10,50)
$Searchformlabel.Size= New-Object System.Drawing.Size(150,50)
$Searchform.Controls.Add($Searchformlabel)


$searchBox1 = New-Object System.Windows.Forms.TextBox
$searchBox1.Text="$DefaultText"
$searchBox1.Location = New-Object System.Drawing.Size (200,50)
$searchBox1.Size= New-Object System.Drawing.Size(150,200)
$Searchform.Controls.Add($searchBox1)



#Button
$searchbutton = New-Object System.Windows.Forms.Button
$searchbutton.Text = "Proceed"
$searchbutton.Location = New-Object System.Drawing.Size(360,50)
$searchbutton.Size= New-Object System.Drawing.Size(80,50)
$searchForm.Controls.Add($searchbutton)
$searchbutton.Add_click({Searchuser})      

$searchform.ShowDialog()
}
#Function Search User
Function Searchuser {
try {
<#
$User=Get-ADUser $searchBox1.Text  -Properties *  |select GivenName ,@{Name="Surname";exp={$_.Surname}},`
                @{Name="SAM";exp={$_.SAMAccountName}} 
$group=$user
#>
$group=Get-ADUser $searchBox1.Text -Properties * |Select GivenName,Surname,SamAccountName,PasswordlastSet,PasswordNeverExpires,MemberOf,Enabled,Created,UserPrincipalName,PasswordExpired,LockedOut -Verbose
New-Object -TypeName psobject -Property @{
GivenName=$($group.GivenName)
Surname=$($group.Surname)
Enabled=$group.Enabled
Created=$group.Created
SamAccountName=$group.SamAccountName
Membership=$group.memberOf
PasswordLastset=$group.PasswordlastSet
PasswordNEverExpires=$group.PasswordNeverExpires
UserPrincipalName=$group.UserPrincipalName
PasswordExpired =$group.PasswordExpired 
LockedOut=$group.LockedOut

}

[System.Windows.Forms.MessageBox]::Show("$group", "User Status","OkCancel","Information")
 }
 catch [System.Management.Automation.RuntimeException]{
[System.Windows.Forms.MessageBox]::Show("Account Not Found", "Not Found ","RetryCancel","Exclamation")}
}

Thanks

1 Like

I am not an expert in PowerShell script so I cannot verify the script instead I will give you the better one. Please try out the below script which will give you the same response you are looking. For by the way the reason for member of property not display because member of property is an array.

$group      = Get-ADUser username -Properties *
$Membership = $group | select-object -ExpandProperty memberof
$grouptable = foreach($table in $group){[PSCustomObject]@{
    'GivenName'            = $table.GivenName
    'Surname'              = $table.Surname
    'Enabled'              = $table.Enabled
    'Created'              = $table.Created
    'SamAccountname'       = $table.SamAccountName
    'Membership'           = $Membership -as [string]
    'PasswordLastSet'      = $table.PasswordlastSet
    'PasswordNeverExpires' = $table.PasswordNeverExpires
    'UserPrincipalName'    = $table.UserPrincipalName
    'PasswordExpired'      = $table.PasswordExpired
    'LockedOut'            = $table.LockedOut
    }
   }
[System.Windows.Forms.MessageBox]::Show("$grouptable", "User Status","OkCancel","Information")
 }
catch [System.Management.Automation.RuntimeException]{
[System.Windows.Forms.MessageBox]::Show("Account Not Found", "Not Found ","RetryCancel","Exclamation")}
}