AD user offboarding GUI tool

Pavan G SPavan G S
7 min read

User offboarding is a critical process in every organization, ensuring that the departure of employees is handled securely and efficiently. To streamline this process and minimize human errors, we'll create a powerful PowerShell tool that not only disables Active Directory (AD) accounts but also removes users from all security groups, moves them to a "Disabled Users" Organizational Unit (OU), and adds an exit date timestamp to their display names.


Let's break down the step-by-step process of how this PowerShell tool for user offboarding works:

Launch the application by either double-clicking it or executing the PowerShell script.

After launching the application, enter the username of the user you wish to offboard and then click on the "Search" button. A pop-up will appear displaying matching usernames for you to select from.

As you can observe in the image below, the application will collect essential user details such as Email, Reporting Manager, Last login, Display Name, and Account Status. These details will be prominently displayed within the application for your reference.

Once you have verified that this is indeed the user you wish to offboard, you can proceed by clicking on the "Disable" button to perform the offboarding process in Active Directory.

After the disable process is completed, a pop-up notification will inform you that the Active Directory account of the specified user has been disabled. Additionally, this notification message will be copied to your clipboard. During this process, the following actions will be performed:

  1. AD Account Disabled: The user's Active Directory account is disabled, ensuring that they can no longer access network resources.

  2. User Moved to Disabled User OU: The user will be transferred to the "Disabled User" Organizational Unit (OU) within Active Directory. This organizational unit is typically used to segregate disabled or offboarded users from the rest of the user accounts.

  3. Removal from Security Groups: The tool will remove the user from all security groups to prevent any unintended access or permissions that may have been associated with their account.

  4. Timestamp Added to Display Name: The current date is added as a timestamp to the user's display name, indicating when the offboarding process was executed. This can be helpful for record-keeping and tracking.

These actions help ensure a secure and well-documented offboarding process while minimizing the chances of errors or oversights.

Upon the completion of the offboarding process, comprehensive logs containing all the relevant details will be automatically generated and saved in a specific location for future reference. These logs will be stored in the directory:

C:\Users\Public

Having these logs readily available is invaluable for auditing, compliance, and record-keeping purposes. It provides transparency and accountability in the offboarding process, ensuring that all steps are executed correctly and in compliance with organizational policies and security protocols.

Here is the full PowerShell code for the tool:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#~~< User Offboarding >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$User_Offboarding = New-Object System.Windows.Forms.Form
$User_Offboarding.ClientSize = New-Object System.Drawing.Size(705, 325)
$User_Offboarding.Text = "User Offboarding"
$User_Offboarding.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$User_Offboarding.MaximizeBox = $false

$base64Icon = "iVBORw0KGgoAAAANSUhEUgAAAEYAAABGCAYAAABxLuKEAAAACXBIWXMAAAsTAAALEwEAmpwYAAAgAElEQVR4nO2bd1SU1/a/9ztJREWYAYY+zNA7Ylc0UaNJTHJNNzHJvekmN73HmGLUxN5iwQb2ghUVUSwgoIBSFCw0sRcGQXqHec/e33XO+4KY3Ptb9/8fZ61nnRlcKO8zn71PUQG6R/foHt2je3SP7tE9ukf36B7do3v8TwNxMzDcJGYU80Yg3A
AoWC8gXAeIa4EwWiUKCNcA4mogXAWIKwHZCiCMBMRIYLgcEJcBsiWA+CcgLgLEhYC4AJDNB8S5gGw2oDwLUP4D0PI7YPt0wPbfANt/BWz7BVjDtw/+nC3fATZ9o9D4FWDjl4ANXwDWf65Q9xlg3SfAaj8GrPkIsOYDwOr3AaveA6x6B7DyXWD33v4fpchnAOkgMNoPSPsAKRaQ9gAJdovXKOZdwGgXEO0EpB2AtB2ItgPSNgHRVkDaorIZGG0EpA3A
aD0grQNGa4FRFCCtAaRVKisAKRKQlgHSEkD6E5AWA9ICYDQPkM25/3PSUmD4B6A8TcEyFbCd84uQiG0/A7ZMAdbyI2DzZGDN3wNr+gZY09fAGr8A1vg5MCHyW2A33oHmeN//IKN9PjBLJLD2BYDtMx/C5k802PAvDTZM1FDDqxqqn6DB+lc0WP+yhupf0FD9ixqqf15DdeM1WDdeQzXPaKjmaQ1WP6nBqjEaqnpcg1WjNXRvlIYqRmiovL9EZeESmv
tKWBoi4e0ACW/5S3jDV8JrRgmveEhY4i7hJXcJC5wlvGgv4Tk7CXO1Eub0kTCrt0QnQMK8cA27OB4sRIC1b2gwS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zi2tGdML1j1rp/zLzez3bODEazaQOtFeos220mfgAL88pIXfHrGBGb10sMBJD4vcHOFPd0dYwm
cPR/jT4AhLPBxhmdEBlpscYLlRD5Geelhu4rMDLPe0hxWedrDS0w4ivbQQ6amFSJMtRBr7QKSnDaxQiTTx2RYiTdawzLMPLPeyUfC2hUg/e4gMdIClAfawMsQGngJXSJ7YW9rzZB+g6wCL/Kxhnp8dzPLRwQwPa5jq1hN+ce8NvxisYIprL/jBxQp+cLOCbw094Ut3K/jc9RH41KM3/NvYBz70sIE3nKygL9jAyX+CZuXjVtKyUb1gwzirB/91eMoL
1pA+XoLdY/RwaLweEsbbQ8J4O+nwcw7S0Rf10rGX9NKxl/VSIueV+yRNcOjk+Kvq61ftpaQJ9tLxVzl2UpJAJyVN4GgfIJHzCsdWOtaFo69opaMTdNKRCTrpMOdlG+ngi1rInAiw66neEPucDuKe7yPtf8FW2vucjbTnWWtp1y8yjXMAAAEOSURBVNO9pR3jOL2kmCd7SVue6CltecJK2vSklbR+rJW09vEeUvQYK2nNGCtp1eM9pchRj0hLHu0BM4
f0gan9rWH2cNv//n8JEsbr4MiTD0PyOICUZwFSnwU48TTAiXEKJ59WSHsGIP0fABnjATKeUzj1PMCpFwBOvwhw+iWA0y8DZL4CkDUBIHMCQNZrADmvA2RPBMji8xsqryvvczrec15Tv/dFhdzXAI6//FDnz3noHwCnXgJIexYg5SmA5PEAx8cDHH0GIGEcwKFxAHFPAMSOBtg1HGDHcIBtEQCbBgKsHwAQHQ6wZgDAssEAS0f1/N/+o0X36B7do3t0
j+7RPbpH9+ge3aN7wP/f4/8AKHqGC2oHEQkAAAAASUVORK5CYII="
$iconBytes = [System.Convert]::FromBase64String($base64Icon)
$iconStream = New-Object System.IO.MemoryStream
$iconStream.Write($iconBytes, 0, $iconBytes.Length)
$User_Offboarding.Icon = [System.Drawing.Icon]::FromHandle((New-Object System.Drawing.Bitmap $iconStream).GetHicon())

# Set form background color and font
$User_Offboarding.BackColor = [System.Drawing.Color]::White
$User_Offboarding.Font = New-Object System.Drawing.Font("Tahoma", 10, [System.Drawing.FontStyle]::Regular)

#~~< disable_btn >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$disable_btn = New-Object System.Windows.Forms.Button
$disable_btn.Font = New-Object System.Drawing.Font("Tahoma", 8.25, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$disable_btn.Location = New-Object System.Drawing.Point(565, 278)
$disable_btn.Size = New-Object System.Drawing.Size(113, 23)
$disable_btn.TabIndex = 11
$disable_btn.Text = "Disable"
$disable_btn.UseVisualStyleBackColor = $true
$disable_btn.add_Click({Disable_btnClick($disable_btn)})
#~~< Display_Name_lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Display_Name_lbl = New-Object System.Windows.Forms.Label
$Display_Name_lbl.Location = New-Object System.Drawing.Point(195, 223)
$Display_Name_lbl.Size = New-Object System.Drawing.Size(305, 23)
$Display_Name_lbl.TabIndex = 10
$Display_Name_lbl.Text = ""
$Display_Name_lbl.add_Click({Display_Name_lblClick($Display_Name_lbl)})
#~~< Label4 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label4 = New-Object System.Windows.Forms.Label
$Label4.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label4.Location = New-Object System.Drawing.Point(27, 223)
$Label4.Size = New-Object System.Drawing.Size(148, 23)
$Label4.TabIndex = 9
$Label4.Text = "Display Name:"
#~~< Last_Loggedon_lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Last_Loggedon_lbl = New-Object System.Windows.Forms.Label
$Last_Loggedon_lbl.Location = New-Object System.Drawing.Point(195, 173)
$Last_Loggedon_lbl.Size = New-Object System.Drawing.Size(305, 23)
$Last_Loggedon_lbl.TabIndex = 8
$Last_Loggedon_lbl.Text = ""
$Last_Loggedon_lbl.add_Click({Last_Loggedon_lblClick($Last_Loggedon_lbl)})
#~~< Label3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label3 = New-Object System.Windows.Forms.Label
$Label3.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label3.Location = New-Object System.Drawing.Point(27, 173)
$Label3.Size = New-Object System.Drawing.Size(148, 23)
$Label3.TabIndex = 7
$Label3.Text = "Last LoggedOn:"
#~~< Reporting_mgr_lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Reporting_mgr_lbl = New-Object System.Windows.Forms.Label
$Reporting_mgr_lbl.Location = New-Object System.Drawing.Point(195, 126)
$Reporting_mgr_lbl.Size = New-Object System.Drawing.Size(305, 23)
$Reporting_mgr_lbl.TabIndex = 6
$Reporting_mgr_lbl.Text = ""
#~~< Label2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label2.Location = New-Object System.Drawing.Point(27, 126)
$Label2.Size = New-Object System.Drawing.Size(148, 23)
$Label2.TabIndex = 5
$Label2.Text = "Reporting Manager:"
#~~< Email_Lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Email_Lbl = New-Object System.Windows.Forms.Label
$Email_Lbl.Location = New-Object System.Drawing.Point(195, 80)
$Email_Lbl.Size = New-Object System.Drawing.Size(305, 23)
$Email_Lbl.TabIndex = 4
$Email_Lbl.Text = ""
$Email_Lbl.add_Click({Email_LblClick($Email_Lbl)})
#~~< Status_Lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$Enabled_lbl = New-Object System.Windows.Forms.Label
$Enabled_lbl.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Enabled_lbl.Location = New-Object System.Drawing.Point(27, 270)
$Enabled_lbl.Size = New-Object System.Drawing.Size(130, 23)
$Enabled_lbl.TabIndex = 12
$Enabled_lbl.Text = "Account Enabled:"


$Check_Enabled_lbl = New-Object System.Windows.Forms.Label
#$Check_Enabled_lbl.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Check_Enabled_lbl.Location = New-Object System.Drawing.Point(195, 270)
$Check_Enabled_lbl.Size = New-Object System.Drawing.Size(200, 23)
$Check_Enabled_lbl.TabIndex = 13
$Check_Enabled_lbl.Text = ""


#~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label1.Location = New-Object System.Drawing.Point(27, 80)
$Label1.Size = New-Object System.Drawing.Size(100, 23)
$Label1.TabIndex = 3
$Label1.Text = "Email:"
#~~< Search_btn >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Search_btn = New-Object System.Windows.Forms.Button
$Search_btn.Location = New-Object System.Drawing.Point(565, 25)
$Search_btn.Size = New-Object System.Drawing.Size(113, 23)
$Search_btn.TabIndex = 2
$Search_btn.Text = "Search"
$Search_btn.UseVisualStyleBackColor = $true
$Search_btn.add_Click({Search_btnClick($Search_btn)})
#~~< username_textbox >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$username_textbox = New-Object System.Windows.Forms.RichTextBox
$username_textbox.Location = New-Object System.Drawing.Point(195, 26)
$username_textbox.Size = New-Object System.Drawing.Size(305, 22)
$username_textbox.TabIndex = 1
$username_textbox.Text = ""
#$username_textbox.add_TextChanged({Username_textboxTextChanged($username_textbox)})
#~~< Username_lbl >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Username_lbl = New-Object System.Windows.Forms.Label
$Username_lbl.Font = New-Object System.Drawing.Font("Tahoma", 10.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Username_lbl.Location = New-Object System.Drawing.Point(27, 25)
$Username_lbl.Size = New-Object System.Drawing.Size(100, 23)
$Username_lbl.TabIndex = 0
$Username_lbl.Text = "Username:"
$User_Offboarding.Controls.Add($disable_btn)
$User_Offboarding.Controls.Add($Display_Name_lbl)
$User_Offboarding.Controls.Add($Label4)
$User_Offboarding.Controls.Add($Last_Loggedon_lbl)
$User_Offboarding.Controls.Add($Label3)
$User_Offboarding.Controls.Add($Reporting_mgr_lbl)
$User_Offboarding.Controls.Add($Label2)
$User_Offboarding.Controls.Add($Email_Lbl)
$User_Offboarding.Controls.Add($Label1)
$User_Offboarding.Controls.Add($Search_btn)
$User_Offboarding.Controls.Add($username_textbox)
$User_Offboarding.Controls.Add($Username_lbl)
$User_Offboarding.Controls.Add($Enabled_lbl)
$User_Offboarding.Controls.Add($Check_Enabled_lbl)

function Main{
    [System.Windows.Forms.Application]::EnableVisualStyles()
    [System.Windows.Forms.Application]::Run($User_Offboarding)
}



function Disable_btnClick( $object ){


function Write-Log {
    param (
        [string]$Message,
        [string]$LogPath
    )

    $LogEntry = "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $Message"
    $LogEntry | Out-File -Append -FilePath $LogPath
}



 $LogPath = "C:\Users\Public\Userooffboard.log"  # Update with the actual path

    # Assuming $username_textbox.Text contains the username of the account to disable

    # Log the start of the process
    Write-Log "Starting the account removal process for $($username_textbox.Text)" -LogPath $LogPath

    # Disable the user account in Active Directory
    Disable-ADAccount -Identity $username_textbox.Text
    Write-Log "Disabled user account: $($username_textbox.Text)" -LogPath $LogPath

    # Move the disabled user account to the specified organizational unit (OU)
    Get-ADUser $username_textbox.Text | Move-ADObject -TargetPath 'OU=Disabled users, OU=Users, DC=Test.local'
    Write-Log "Moved user account: $($username_textbox.Text) to OU=Disabled users, OU=Users, DC=Test.local" -LogPath $LogPath

    # Get the user's current group memberships and remove them from those groups
    $userGroups = Get-ADUser -Identity $username_textbox.Text -Properties MemberOf |
                  Select-Object -ExpandProperty MemberOf

    foreach ($group in $userGroups) {
        # Get the group's information
        $groupInfo = Get-ADGroup $group

        # Remove the user from the group
        Remove-ADGroupMember -Identity $group -Members $username_textbox.Text -Confirm:$false

        # Log the removal
        Write-Log "Removed $($username_textbox.Text) from group: $($groupInfo.Name)" -LogPath $LogPath
    }

$todayDate = Get-Date -Format "dd-MM-yy"

$identity = $username_textbox.Text

$newDisplayName = "[Exited on_$todayDate]"

$currentUser = Get-ADUser -Identity $identity
$currentDisplayName = $currentUser.Name

$combinedDisplayName = "$currentDisplayName $newDisplayName"

Set-ADUser -Identity $identity -DisplayName $combinedDisplayName


[string]$user = $username_textbox.Text


[System.Windows.Forms.MessageBox]::Show("User $user account has been disabled ", "Information", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
Set-Clipboard -Value "AD account disabled for the user $user and removed from all the security groups."

}


function Search_btnClick( $object ){
    # Get the entered username from the textbox
    $enteredUsername = $username_textbox.Text

    # Simulate fetching matching usernames from Active Directory
    # Replace this with your actual logic to query Active Directory
    $matchingUsernames = Get-ADUser -Filter "SamAccountName -like '$enteredUsername*'" |
                         Select-Object -ExpandProperty SamAccountName

    # Create a new form for selecting from matching usernames
    $Username_Select = New-Object System.Windows.Forms.Form
    $Username_Select.ClientSize = New-Object System.Drawing.Size(300, 200)
    $Username_Select.Text = "Select Username"
    $Username_Select.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$Username_Select.MaximizeBox = $false

    # Set form background color and font
$Username_Select.BackColor = [System.Drawing.Color]::White
$Username_Select.Font = New-Object System.Drawing.Font("Tahoma", 10, [System.Drawing.FontStyle]::Regular)

    $ListBox = New-Object System.Windows.Forms.ListBox
    $ListBox.Location = New-Object System.Drawing.Point(50, 50)
    $ListBox.Size = New-Object System.Drawing.Size(200, 100)

    # Populate the list box with matching usernames
    $ListBox.Items.AddRange($matchingUsernames)

    $Select_Button = New-Object System.Windows.Forms.Button
    $Select_Button.Location = New-Object System.Drawing.Point(100, 160)
    $Select_Button.Size = New-Object System.Drawing.Size(100, 25)
    $Select_Button.Text = "Select"
    $Select_Button.Add_Click({
        $selectedUsername = $ListBox.SelectedItem
        $username_textbox.Text = $selectedUsername
        $Username_Select.Close()

        # Retrieve user information based on the selected username
        $aduser = Get-AdUser -Identity $selectedUsername -Properties Manager
        $manager = Get-AdUser $aduser.Manager -Properties DisplayName

        $aduser_email = Get-ADUser -Identity $selectedUsername -properties mail | Select -ExpandProperty mail

        $aduser_lastlogon = Get-ADUser -Identity $selectedUsername -Properties "LastLogonDate" | Select-Object -ExpandProperty LastLogonDate

        $aduser_enabled = Get-ADUser $selectedUsername -properties enabled,PasswordExpired | select -ExpandProperty enabled

        $aduser_displayname = Get-AdUser -Identity $selectedUsername -Properties * | Select -ExpandProperty DisplayName

        #$aduser_displayname = $aduser.Name
        #$aduser_email = $aduser.mail
        #$aduser_lastlogon = $aduser.LastLogonDate

        # Populate labels with retrieved user information
        $Display_Name_lbl.Text = $aduser_displayname
        $Last_Loggedon_lbl.Text = $aduser_lastlogon
        $Reporting_mgr_lbl.Text = $manager.DisplayName
        $Email_Lbl.Text = $aduser_email
        $Check_Enabled_lbl.Text = $aduser_enabled

        # Set the text color of the Enabled label based on the account status
if ($aduser_enabled -eq "True") {
    $Check_Enabled_lbl.ForeColor = [System.Drawing.Color]::Green
} else {
    $Check_Enabled_lbl.ForeColor = [System.Drawing.Color]::Red
}
    })

    $Username_Select.Controls.Add($ListBox)
    $Username_Select.Controls.Add($Select_Button)

    # Show the username selection form
    $Username_Select.ShowDialog()
}



Main

Here is the PowerShell code for your reference, and you can modify the following line to tailor it to your specific environment:

Get-ADUser $username_textbox.Text | Move-ADObject -TargetPath 'OU=Disabled users, OU=Users, DC=Test.local'
Write-Log "Moved user account: $($username_textbox.Text) to OU=Disabled users, OU=Users, DC=Test.local" -LogPath $LogPath

0
Subscribe to my newsletter

Read articles from Pavan G S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pavan G S
Pavan G S

IT expert mastering PowerShell automation, solution architecture & emerging tech. Crafting efficient solutions for seamless operations. Let's innovate together!