Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tommymaynard/b833e7fa33dd76f2484b73db58a7d281 to your computer and use it in GitHub Desktop.
Save tommymaynard/b833e7fa33dd76f2484b73db58a7d281 to your computer and use it in GitHub Desktop.
Active Directory User Lookup Form
<#
TechNet Contribution: Active Directory User Lookup Form
Previous link: https://gallery.technet.microsoft.com/Active-Directory-User-d8ee6a0c
Downloaded: 1,034 times (as of 05/21/2020)
This PowerShell script creates a PowerShell form to search for users in Active Directory by Username (SamAccountName).
The form returns a user's Name, Distinguished Name, Email, Title, and Office Phone. You can read more about the form's
development at http://tommymaynard.com/script-sharing-active-directory-user-lookup-form-2015.
Update to 2.1: 2015-07-23 (see code for changes).
#>
Function Search-ADUser {
<#
.SYNOPSIS
Creates a PowerShell form to search for users in Active Directory by Username.
.DESCRIPTION
Creates a PowerShell form to search for users in Active Directory by Username (SamAccountName). The form returns a user's Name, Distinguished Name, Email, Title, and Office Phone.
.NOTES
Author: Tommy Maynard
Email: tommy@tommymaynard.com
Site: http://tommymaynard.com
Last Update: 2.1: 2015-07-23
- Added AutoEllipsis to Department and Title results if greater than 30 characters (added logic). Required add of tooltip and removal of tooltip when necessary.
- Added logic for Distinguished Name if greater than 30 characters (already had add/remove of tooltip).
- Created function to clear all tooltips at once.
- Corrected version in about field.
- Set buttons as .FlatStyle = 'Popup'.
- Trim() on $TextboxUsername.Text.
#>
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$MainFont = New-Object System.Drawing.Font('Calibri',10,[System.Drawing.FontStyle]::Regular)
##### Functions.
Function Clear-ToolTips {
$ToolTip.SetToolTip($LabelDistinguishedNameResults,$null)
$ToolTip.SetToolTip($LabelJobTitleResults,$null)
$ToolTip.SetToolTip($LabelDepartmentResults,$null)
$ToolTip.SetToolTip($LabelDistinguishedNameResults,$null)
}
Function Get-ADUserModified {
If ($TextboxUsername.Text -ne '') {
$TextboxUsername.Text = ($TextboxUsername.Text).Trim()
try {
$User = Get-ADUser -Identity $TextboxUsername.Text -Properties mail,Title,Department,OfficePhone,eduPersonPrimaryAffiliation
Clear-ToolTips
$TextboxUsername.BackColor = 'White'
$LabelNameResults.Text = $User.Name
$LabelNameResults.Font = $MainFont
$LabelDistinguishedNameResults.Text = $User.DistinguishedName
If ($LabelDistinguishedNameResults.Text.Length -gt 30) {
$LabelDistinguishedNameResults.AutoEllipsis = $true
$ToolTip.SetToolTip($LabelDistinguishedNameResults,$User.DistinguishedName)
}
$LabelEmailResults.Text = $User.Mail
$LabelJobTitleResults.Text = $User.Title
If ($LabelJobTitleResults.Text.Length -gt 30) {
$LabelJobTitleResults.AutoEllipsis = $true
$ToolTip.SetToolTip($LabelJobTitleResults,$User.Title)
}
$LabelDepartmentResults.Text = $User.Department
If ($LabelDepartmentResults.Text.Length -gt 30) {
$LabelDepartmentResults.AutoEllipsis = $true
$ToolTip.SetToolTip($LabelDepartmentResults,$User.Title)
}
$LabelOfficePhoneResults.Text = $User.OfficePhone
$ButtonClear.Focus()
} catch {
$LabelNameResults.Text = "Cannot Locate User ($($TextboxUsername.Text))"
$TextboxUsername.BackColor = 'White'
$LabelDistinguishedNameResults.Text = $null
$LabelEmailResults.Text = $null
$LabelJobTitleResults.Text = $null
$LabelDepartmentResults.Text = $null
$LabelOfficePhoneResults.Text = $null
$TextboxUsername.SelectAll()
$TextboxUsername.Focus()
Clear-ToolTips
}
} Else {
$TextboxUsername.BackColor = 'Pink'
$TextboxUsername.Focus()
$LabelNameResults.Text = $null
$LabelDistinguishedNameResults.Text = $null
$LabelEmailResults.Text = $null
$LabelJobTitleResults.Text = $null
$LabelDepartmentResults.Text = $null
$LabelOfficePhoneResults.Text = $null
Clear-ToolTips
}
}
Function Get-About {
##### Form (About).
$FormAbout = New-Object System.Windows.Forms.Form
$FormAbout.FormBorderStyle = 'Fixed3D'
$FormAbout.MaximizeBox = $false
$FormAbout.MinimizeBox = $false
$FormAbout.Size = New-Object System.Drawing.Size(300,200)
$FormAbout.StartPosition = 'CenterParent'
$FormAbout.SizeGripStyle = 'Hide'
$FormAbout.Text = 'About'
##### Label (About).
$LabelAbout = New-Object System.Windows.Forms.Label
$LabelAbout.Font = $MainFont
$LabelAbout.Location = New-Object System.Drawing.Size(20,30)
$LabelAbout.Text = "Active Directory User Lookup`r`n`r`nWritten by Tommy Maynard`r`nVersion: 2.1`r`nEmail: tommy@tommymaynard.com"
$LabelAbout.AutoSize = $True
$FormAbout.Controls.Add($LabelAbout)
$FormAbout.ShowDialog()
}
Function Clear-Username {
$TextboxUsername.Text = $null
$LabelNameResults.Text = $null
$LabelDistinguishedNameResults.Text = $null
$LabelEmailResults.Text = $null
$LabelJobTitleResults.Text = $null
$LabelDepartmentResults.Text = $null
$LabelOfficePhoneResults.Text = $null
$TextboxUsername.Focus()
Clear-ToolTips
}
Function Close-Form {
$Form.Close()
}
##### Test for Active Directory Module.
try {
Import-Module -Name ActiveDirectory -ErrorAction Stop
$PassingVariable = 0
} catch [System.IO.FileNotFoundException] {
$PassingVariable = 1
} catch {
$PassingVariable = 2
}
##### Begin Creating Form.
If ($PassingVariable -eq 0) {
##### Form
$Form = New-Object System.Windows.Forms.Form
If (Test-Path -Path "$env:SystemRoot\System32\dsadmin.dll") {
$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("$env:SystemRoot\System32\dsadmin.dll")
} ElseIf (Test-Path "$PSHOME\powershell.exe") {
$Form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("$PSHOME\powershell.exe")
}
$Form.Size = New-Object System.Drawing.Size(400,295)
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
$Form.StartPosition = 'CenterScreen'
$Form.SizeGripStyle = 'Hide'
$Form.Text = "Active Directory User Lookup ($((Get-ADDomain | Select-Object -ExpandProperty Name).ToUpper()))"
##### Label (Username)
$LabelUsername = New-Object System.Windows.Forms.Label
$LabelUsername.Font = $MainFont
$LabelUsername.Location = New-Object System.Drawing.Size(10,13)
$LabelUsername.AutoSize = $True
$LabelUsername.Text = 'Username'
$Form.Controls.Add($LabelUsername)
##### Textbox (Username)
$TextboxUsername = New-Object System.Windows.Forms.Textbox
$TextboxUsername.BorderStyle = 'FixedSingle'
$TextboxUsername.Font = $MainFont
$TextboxUsername.Location = New-Object System.Drawing.Size(75,10)
$TextboxUsername.Size = New-Object System.Drawing.Size(245)
$TextboxUsername.MaxLength = 30
$Form.Controls.Add($TextboxUsername)
##### Button (Check)
$ButtonCheck = New-Object System.Windows.Forms.Button
$ButtonCheck.FlatStyle = 'Popup'
$ButtonCheck.Font = $MainFont
$ButtonCheck.Location = New-Object System.Drawing.Size(325,9)
$ButtonCheck.Size = New-Object System.Drawing.Size(50,26)
$ButtonCheck.Text = 'Check'
$Form.Controls.Add($ButtonCheck)
$ButtonCheck.Add_Click({Get-ADUserModified})
$Form.AcceptButton = $ButtonCheck
##### Label (Name)
$LabelName = New-Object System.Windows.Forms.Label
$LabelName.Font = $MainFont
$LabelName.Location = New-Object System.Drawing.Size(20,55)
$LabelName.Size = New-Object System.Drawing.Size(80,20)
$LabelName.Text = 'Name'
$Form.Controls.Add($LabelName)
##### Label (Name Results)
$LabelNameResults = New-Object System.Windows.Forms.Label
$LabelNameResults.BackColor = 'LightGray'
$LabelNameResults.Font = $MainFont
$LabelNameResults.Location = New-Object System.Drawing.Size(105,55)
$LabelNameResults.Size = New-Object System.Drawing.Size(260,20)
$LabelNameResults.Text = $null
$Form.Controls.Add($LabelNameResults)
##### Label (DistinguishedName)
$LabelDistinguishedName = New-Object System.Windows.Forms.Label
$LabelDistinguishedName.Font = $MainFont
$LabelDistinguishedName.Location = New-Object System.Drawing.Size(20,80)
$LabelDistinguishedName.Size = New-Object System.Drawing.Size(80,20)
$LabelDistinguishedName.Text = 'DN'
$Form.Controls.Add($LabelDistinguishedName)
##### Label (DistinguishedName Results)
$LabelDistinguishedNameResults = New-Object System.Windows.Forms.Label
$LabelDistinguishedNameResults.BackColor = 'LightGray'
$LabelDistinguishedNameResults.Font = $MainFont
$LabelDistinguishedNameResults.Location = New-Object System.Drawing.Size(105,80)
$LabelDistinguishedNameResults.Size = New-Object System.Drawing.Size(260,20)
$LabelDistinguishedNameResults.Text = $null
$Form.Controls.Add($LabelDistinguishedNameResults)
##### Label (Email)
$LabelEmail = New-Object System.Windows.Forms.Label
$LabelEmail.Font = $MainFont
$LabelEmail.Location = New-Object System.Drawing.Size(20,105)
$LabelEmail.Size = New-Object System.Drawing.Size(80,20)
$LabelEmail.Text = 'Email'
$Form.Controls.Add($LabelEmail)
##### Label (Email Results)
$LabelEmailResults = New-Object System.Windows.Forms.Label
$LabelEmailResults.BackColor = 'LightGray'
$LabelEmailResults.Font = $MainFont
$LabelEmailResults.Location = New-Object System.Drawing.Size(105,105)
$LabelEmailResults.Size = New-Object System.Drawing.Size(260,20)
$LabelEmailResults.Text = $null
$Form.Controls.Add($LabelEmailResults)
##### Label (Job Title)
$LabelJobTitle = New-Object System.Windows.Forms.Label
$LabelJobTitle.Font = $MainFont
$LabelJobTitle.Location = New-Object System.Drawing.Size(20,130)
$LabelJobTitle.Size = New-Object System.Drawing.Size(80,20)
$LabelJobTitle.Text = 'Job Title'
$Form.Controls.Add($LabelJobTitle)
##### Label (Job Title Results)
$LabelJobTitleResults = New-Object System.Windows.Forms.Label
$LabelJobTitleResults.BackColor = 'LightGray'
$LabelJobTitleResults.Font = $MainFont
$LabelJobTitleResults.Location = New-Object System.Drawing.Size(105,130)
$LabelJobTitleResults.Size = New-Object System.Drawing.Size(260,20)
$LabelJobTitleResults.Text = $null
$Form.Controls.Add($LabelJobTitleResults)
##### Label (Department)
$LabelDepartment = New-Object System.Windows.Forms.Label
$LabelDepartment.Font = $MainFont
$LabelDepartment.Location = New-Object System.Drawing.Size(20,155)
$LabelDepartment.Size = New-Object System.Drawing.Size(80,20)
$LabelDepartment.Text = 'Department'
$Form.Controls.Add($LabelDepartment)
##### Label (Department Results)
$LabelDepartmentResults = New-Object System.Windows.Forms.Label
$LabelDepartmentResults.BackColor = 'LightGray'
$LabelDepartmentResults.Font = $MainFont
$LabelDepartmentResults.Location = New-Object System.Drawing.Size(105,155)
$LabelDepartmentResults.Size = New-Object System.Drawing.Size(260,20)
$LabelDepartmentResults.Text = $null
$Form.Controls.Add($LabelDepartmentResults)
##### Label (Office Phone)
$LabelOfficePhone = New-Object System.Windows.Forms.Label
$LabelOfficePhone.Font = $MainFont
$LabelOfficePhone.Location = New-Object System.Drawing.Size(20,180)
$LabelOfficePhone.Size = New-Object System.Drawing.Size(80,20)
$LabelOfficePhone.Text = 'Office Phone'
$Form.Controls.Add($LabelOfficePhone)
##### Label (Office Phone Results)
$LabelOfficePhoneResults = New-Object System.Windows.Forms.Label
$LabelOfficePhoneResults.BackColor = 'LightGray'
$LabelOfficePhoneResults.Font = $MainFont
$LabelOfficePhoneResults.Location = New-Object System.Drawing.Size(105,180)
$LabelOfficePhoneResults.Size = New-Object System.Drawing.Size(260,20)
$LabelOfficePhoneResults.Text = $null
$Form.Controls.Add($LabelOfficePhoneResults)
##### Groupbox (Results)
$Groupbox = New-Object System.Windows.Forms.GroupBox
$Groupbox.Location = New-Object System.Drawing.Size(10,40)
$Groupbox.Size = New-Object System.Drawing.Size(365,170)
$Form.Controls.Add($Groupbox)
##### Button (About)
$ButtonAbout = New-Object System.Windows.Forms.Button
$ButtonAbout.FlatStyle = 'Popup'
$ButtonAbout.Font = $MainFont
$ButtonAbout.Location = New-Object System.Drawing.Size(10,217)
$ButtonAbout.Size = New-Object System.Drawing.Size(50,26)
$ButtonAbout.Text = 'About'
$Form.Controls.Add($ButtonAbout)
$ButtonAbout.Add_Click({Get-About})
##### Button (Clear)
$ButtonClear = New-Object System.Windows.Forms.Button
$ButtonClear.FlatStyle = 'Popup'
$ButtonClear.Font = $MainFont
$ButtonClear.Location = New-Object System.Drawing.Size(266,217)
$ButtonClear.Size = New-Object System.Drawing.Size(50,26)
$ButtonClear.Text = 'Clear'
$Form.Controls.Add($ButtonClear)
$ButtonClear.Add_Click({Clear-Username})
##### Label (Vertical Divider)
$LabelVerticalDivider = New-Object System.Windows.Forms.Label
$LabelVerticalDivider.BackColor = 'LightGray'
$LabelVerticalDivider.Location = New-Object System.Drawing.Size(320,218)
$LabelVerticalDivider.Size = New-Object System.Drawing.Size(1,24)
$Form.Controls.Add($LabelVerticalDivider)
##### Button (Cancel)
$ButtonCancel = New-Object System.Windows.Forms.Button
$ButtonCancel.FlatStyle = 'Popup'
$ButtonCancel.Font = $MainFont
$ButtonCancel.Location = New-Object System.Drawing.Size(325,217)
$ButtonCancel.Size = New-Object System.Drawing.Size(50,26)
$ButtonCancel.Text = 'Close'
$Form.Controls.Add($ButtonCancel)
$ButtonCancel.Add_Click({Close-Form})
$Form.CancelButton = $ButtonCancel
##### Tool Tips
$ToolTip = New-Object System.Windows.Forms.ToolTip
$ToolTip.AutomaticDelay = 0
$ToolTip.SetToolTip($LabelUsername,'SamAccountName')
$ToolTip.SetToolTip($LabelDistinguishedName,'Distinguished Name')
##### Show Dialog
$Form.ShowDialog()
} Else {
##### Form
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
$Form.StartPosition = 'CenterScreen'
$Form.SizeGripStyle = 'Hide'
$Form.Text = 'Error'
##### Label (Failure Message)
$LabelFailureMessage = New-Object System.Windows.Forms.Label
$LabelFailureMessage.Font = $MainFont
If ($PassingVariable -eq 1) {
$LabelFailureMessage.Text = "Error:`r`n`r`nCannot load Windows PowerShell`r`n Active Directory Module. Please ensure`r`n that it is installed on this computer."
} ElseIf ($PassingVariable -eq 2) {
$LabelFailureMessage.Text = "Error:`r`n`r`nUnknown error."
}
$LabelFailureMessage.AutoSize = $true
$LabelFailureMessage.Location = New-Object System.Drawing.Size(20,20)
$Form.Controls.Add($LabelFailureMessage)
##### Button (About)
$ButtonAbout = New-Object System.Windows.Forms.Button
$ButtonAbout.FlatStyle = 'Popup'
$ButtonAbout.Font = $MainFont
$ButtonAbout.Location = New-Object System.Drawing.Size(155,120)
$ButtonAbout.Size = New-Object System.Drawing.Size(50,26)
$ButtonAbout.Text = 'About'
$Form.Controls.Add($ButtonAbout)
$ButtonAbout.Add_Click({Get-About})
##### Label (Vertical Divider)
$LabelVerticalDivider = New-Object System.Windows.Forms.Label
$LabelVerticalDivider.BackColor = 'LightGray'
$LabelVerticalDivider.Location = New-Object System.Drawing.Size(210,121)
$LabelVerticalDivider.Size = New-Object System.Drawing.Size(1,23)
$Form.Controls.Add($LabelVerticalDivider)
##### Button (Close)
$ButtonClose = New-Object System.Windows.Forms.Button
$ButtonClose.FlatStyle = 'Popup'
$ButtonClose.Font = $MainFont
$ButtonClose.Location = New-Object System.Drawing.Size(215,120)
$ButtonClose.Size = New-Object System.Drawing.Size(50,26)
$ButtonClose.Text = 'Close'
$Form.Controls.Add($ButtonClose)
$ButtonClose.Add_Click({Close-Form})
##### Show Dialog
$Form.ShowDialog()
}
}
Search-ADUser
@tommymaynard
Copy link
Author

image

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