Get-userinfo

This one script will allow your helpdesk team to perform their daily AD job without having to open ADUC or AC.

handsome technical support operator working on computer

It can allow them to search for a specific user – by using username, last and firstname or employee ID as search criteria – reset/unlock password, find group membership and computer information.. So let’s get it on..

This function will show a little menu, the old fashion way, where we can select which criteria we wants to use to look for a specific user.  If for example, you select 1, it will then ask you to enter a username.. easy bunny..


function search-menu
{
$script:Useraccount = $null
$script:userinfo =$null
cls
write-host " ------------------------------------ "
write-host "|       User lookup tool             |"
write-host " ------------------------------------ "
write-host " ------------------------------------ "
write-host "|1. Search with username             |"
write-host "|2. Search with Lastname & firstname |"
write-host "|3. search with email address        |"
write-host "|4. search with Oracle ID            |"
write-host "|Q. Quit...                          |"
write-host " ------------------------------------ "
$choice=read-host "Please make a selection"
switch  ($Choice)
        {
        "1" {$user = Read-Host "Please enter username";$filter = 'SamAccountName -like "' + $user + '"';$notfound= $user}
        "2" {$lastname = Read-Host "Please enter Lastname";$firstname = Read-Host "Please enter Firstname"
             $filter = 'surname -like "' + $lastname + '*" -and givenname -like "' + $firstname + '*"';$notfound= "$lastname, $firstname"}
        "3" {$mail= read-host "Please enter email address";$filter = 'mail -like "' + $mail + '*"';$notfound= $mail}
        "4" {$Oracleid= read-host "Please enter Oracle ID";$filter = 'employeeid -like "' + $oracleid + '"';$notfound= $Oracleid}
        "Q" {exit}
        default {exit}
        } 

$Useraccount = get-aduser -searchbase "OU=ContosoUsers,DC=ad,DC=Contoso,DC=com"`
                   –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"`
                   -Filter $filter
switch (($useraccount | measure).count)
       {
        {$_ -eq 0} {cls;write-host -ForegroundColor White -BackgroundColor Red "user $notfound not found";pause;search-menu}
        {$_ -eq 1} {$user = $Useraccount.samaccountname;cls;menu}
        {$_ -gt 1 -and $_ -cle 5} {cls;Write-Host -ForegroundColor Yellow "multiple users found with the value you entered";selectuser}
        {$_ -gt 5} {cls;write-host -ForegroundColor White -BackgroundColor Red "too much results return, abording...";pause;search-menu}
       }
}

We then search in AD with the criteria provided. Depending on the number of results, different options will be available, according to the second switch in use there. If there are between 2 and 5 results, another menu will show, allow you to select which user you looking for :

function selectuser
{
$number = $null
foreach ($account in $Useraccount)
{
$number = 1+$number
New-Variable -Name "User$number" -Value $account.samaccountname
write-host "|$number. Sam = "$account.samaccountname" ; email ="$account.mail" ; Employee ID = "$account.employeeid""
}
write-host ""
$choice=read-host "Please make a selection (Q to quit)"
switch  ($choice)
        {
        "1" {$user = $user1}
        "2" {$user = $user2}
        "3" {$user = $user3}
        "4" {$user = $user4}
        "5" {$user = $user5}
        "Q" {exit}
        default {exit}
        } 

$filter = 'SamAccountName -like "' + $user + '"'
$Useraccount = get-aduser -searchbase "OU=ContosoUsers,DC=ad,DC=Contoso,DC=com"`
                   –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"`
                   -Filter $filter

switch (($useraccount | measure).count)
       {
        {$_ -eq 0} {cls;write-host -ForegroundColor White -BackgroundColor Red "issue, abording...";pause;search-menu}
        {$_ -eq 1} {$user = $Useraccount.samaccountname;cls;menu}
}
}

Once you select the user you looking for, another menu will show up. It will show basic information on the selected user’s account, and give you multiple choices like view details information on the user’s account, reset password …

function menu
{
$userinfo= $useraccount | select-object "surname","givenname","SamAccountName","employeeid","mail","Description","AccountExpirationDate","LockedOut", @{Name="Password Expiry Date";`
Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}

write-host " --------------------------------- "
write-host "|       User lookup tool          |"
write-host " --------------------------------- "
write-host " Current user: $user"
write-host " --------------------------------- "
$userinfo
write-host " --------------------------------- "
write-host "|1. User Account information      |"
write-host "|2. Password reset                |"
write-host "|3. Computer information          |"
write-host "|4. Select another user           |"
write-host "|Q. Quit...                       |"
write-host " --------------------------------- "
$choice=read-host "Please make a selection"

switch  ($Choice)
        {
        "1" {cls;Groups}
        "2" {checkpassword}
        "3" {cls;computeraccount}
        "4" {cls;search-menu}
        "Q" {exit}
        default {menu}
        }
}

Now, let’s go one by one with those options. The first choice will send you to the Groups function. This function will look at the memberof property of the user account and get information about the different groups that user is member of (Name, description, Managedby. If a group starts with “DL-” , we will then deduce it is a distribution list, else it’s a group. This will help when generating our html report.

function Groups
{
write-host -ForegroundColor Yellow "Looking for Groups information..."
$getmembership = $Useraccount | Select -ExpandProperty memberof
$script:group = $getmembership | %{Get-ADGroup $_ -Properties name,description,managedby} | select Name,Description,Managedby | Sort-Object -Property name
$DL.Clear()
$grp.Clear()

$script:DL = @()
$script:grp = @()
$group | foreach {
$name = $_.name
if ($name.StartsWith("DL-")){$DL += $_}
else {$grp += $_}
                 }
UserReport
}

We then go to the UserReport function, which will format those information into an html webpage and open it. Finally we go back to our Menu function.

function UserReport
{
write-host -ForegroundColor Green "generating report..."
$file = "c:\temp\test.htm"
if ((test-path $file) -eq "true"){remove-item -Path $file -Force}

$Style = "

    BODY{background-color:#FFFFFF;}
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
h2, th { text-align: center;font-family: Segoe UI } 

"
$1 = $userinfo | ConvertTo-HTML -As List -fragment -PreContent '</pre>
<h2>User Account Info</h2>
' $3 = $DL | ConvertTo-Html -Fragment -PreContent '
<h2>Distribution List</h2>
' $4 = $grp | ConvertTo-Html -Fragment -PreContent '
<h2>Groups</h2>
' ConvertTo-HTML -Head $Style -Body "$1 $4 $3" -Title "Contoso User Account Information" | Out-File $file Invoke-Expression C:\temp\Test.htm menu }

The second choice of our Menu is Password reset, which send us to our CheckPassword menu. Another sub-menu to let you view if account is locked or password expired, and select an option accordingly.

function checkpassword
{
cls
$userinfo

if (($userinfo.'Password Expiry Date') -cle (get-date)){write-host "password has expired!" -ForegroundColor White -BackgroundColor Red}
else {write-host "password has not expired" -foregroundcolor green }

if ($userinfo.lockedout){write-host "Account is currently locked out" -ForegroundColor White -BackgroundColor Red}
else {write-host "Account is not locked out" -ForegroundColor Green}

write-host " --------------------------------- "
write-host "|1. Reset password                |"
write-host "|2. Unlock Account                |"
write-host "|3. Go back to main menu          |"
write-host "|Q. Quit...                       |"
write-host " --------------------------------- "
$question = read-host "Please make a selection"

switch ($question)
        {
        "1" {password}
        "2" {unlock-account}
        "3" {cls;menu}
        "Q" {write-host "exit...";pause; exit}
        default {write-host "aborting..."; exit}}
 }

The password function will generate and set a new password for the selected user. password format is Firstletter of firstname . Firstletter of lastname _ employeeID. Once set, password will be copied to your clipboard, if needed a pre-formatted email can be generated:

function password
{
$sam = $userinfo.SamAccountName
$gn= ($userinfo.GivenName).ToUpper()
$surname= ($userinfo.Surname).ToUpper()
$random= random -minimum 100000 -Maximum 999999
if ($userinfo.EmployeeID){$number = $userinfo.EmployeeID}
else {$number = $random}
$password = $gn.Substring(0,1) +"."+ $surname.Substring(0,1) +"_"+"$number"
Set-ADAccountPassword -Identity $sam  -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $password -Force)
$password | clip.exe
write-host -ForegroundColor Green "new password is $password and have been copied to your clipboard"
$question = read-host "Would you like to generate email? (Y/N/Q)"

    switch  ($question)
        {
        "Y" {email}
        "N" {$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof";menu}
        default {exit}
        }
}

Just a quick look at the email function :

function email
{
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.SentOnBehalfOfName = "Helpdesk@Contoso.com"
$mail.to = $user.mail
$mail.Subject = "Password reset request"
$mail.Body = "Hi,

Your new password is : $password

Contoso Helpdesk Team
"
$inspector = $mail.GetInspector
$inspector.Display()
cls
$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"
menu
}

The Unlock-Account will simply unlock the user account, not much to say here :

function unlock-account
{
$sam = $userinfo.SamAccountName
Unlock-ADAccount $sam

if ((get-aduser $sam -properties LockedOut).lockedout){write-host "Account still locked out, please try again" -ForegroundColor White -BackgroundColor Red}
else {write-host "Account has been unlocked" -ForegroundColor Green}

$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"
menu
}

The third option of our main menu function will look for computer information. Our search criteria is to look for any computer name that contains the username of the user in 3 different OU. Obviously, this will need to be changed according to your computer naming convention and Computer location in AD.

function ComputerAccount
{
cls
write-host -ForegroundColor Yellow "Looking for Computer information..."
$username = "$user"
$script:Computer= @()
$Computer += Get-ADComputer -SearchBase "OU=OU1,OU=ContosoComputers,DC=ad,DC=Contoso,DC=com" -Server Contoso-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName
$Computer += Get-ADComputer -SearchBase "OU=OU2,DC=ad,DC=Contoso,DC=com" -Server Contoso-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName
$Computer += Get-ADComputer -SearchBase "OU=OU3,DC=ad,DC=Contoso,DC=com" -Server Contoso-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName

ComputerReport
}

Finally, we go to our ComputerReport function that will format the results into an HTML webpage.

function ComputerReport
{
write-host -ForegroundColor Green "generating report..."
$file = "c:\temp\test.htm"
if ((test-path $file) -eq "true"){remove-item -Path $file -Force}

$Style = "

    BODY{background-color:#FFFFFF;}
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
h2, th { text-align: center;font-family: Segoe UI } 

"
$2 = $Computer | ConvertTo-Html -Fragment -PreContent '</pre>
<h2>Computer Info</h2>
' ConvertTo-HTML -Head $Style -Body "$2" -Title "Contoso Computer Account Information" | Out-File $file Invoke-Expression C:\temp\Test.htm menu }

So, that’s it, one long script, build as a friendly 90’s style tool to assist your helpdesk team, perform their daily ad job more easily and faster than the ADUC/AC 🙂 . Below is the full script.

Thank you for reading 😉

Import-Module ActiveDirectory -Cmdlet get-aduser,get-adcomputer,unlock-adaccount,get-adgroup,Set-ADAccountPassword
$ErrorActionPreference = "silentlycontinue"

function Groups
{
write-host -ForegroundColor Yellow "Looking for Groups information..."
$getmembership = $Useraccount | Select -ExpandProperty memberof
$script:group = $getmembership | %{Get-ADGroup $_ -Properties name,description,managedby} | select Name,Description,Managedby | Sort-Object -Property name
$DL.Clear()
$grp.Clear()

$script:DL = @()
$script:grp = @()
$group | foreach {
$name = $_.name
if ($name.StartsWith("DL-")){$DL += $_}
else {$grp += $_}
                 }
UserReport
}

function ComputerAccount
{
cls
write-host -ForegroundColor Yellow "Looking for Computer information..."
$username = "$user"
$script:Computer= @()
$Computer += Get-ADComputer -SearchBase "OU=OU1,OU=ContosoComputers,DC=ad,DC=Contoso,DC=com" -Server CONTOSO-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName
$Computer += Get-ADComputer -SearchBase "OU=OU2,DC=ad,DC=Contoso,DC=com" -Server CONTOSO-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName
$Computer += Get-ADComputer -SearchBase "OU=OU3,DC=ad,DC=Contoso,DC=com" -Server CONTOSO-DC01 -Properties operatingsystem,whenChanged,CanonicalName -Filter ('Name -like "*' + $username + '*"') | select Name, Enabled,OperatingSystem,WhenChanged,CanonicalName

ComputerReport
}

function UserReport
{
write-host -ForegroundColor Green "generating report..."
$file = "c:\temp\test.htm"
if ((test-path $file) -eq "true"){remove-item -Path $file -Force}

$Style = "
<style>
    BODY{background-color:#FFFFFF;}
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
h2, th { text-align: center;font-family: Segoe UI }
</style>
"
$1 = $userinfo | ConvertTo-HTML -As List -fragment -PreContent '
<h2>User Account Info</h2>
'
$3 = $DL | ConvertTo-Html -Fragment -PreContent '
<h2>Distribution List</h2>
'
$4 = $grp | ConvertTo-Html -Fragment -PreContent '
<h2>Groups</h2>
'
ConvertTo-HTML -Head $Style -Body "$1 $4 $3" -Title "Contoso User Account Information" | Out-File $file
Invoke-Expression C:\temp\Test.htm
menu
}

function ComputerReport
{
write-host -ForegroundColor Green "generating report..."
$file = "c:\temp\test.htm"
if ((test-path $file) -eq "true"){remove-item -Path $file -Force}

$Style = "
<style>
    BODY{background-color:#FFFFFF;}
table { margin: auto; font-family: Segoe UI; box-shadow: 10px 10px 5px #888; border: thin ridge grey; }
th { background: #0046c3; color: #fff; max-width: 400px; padding: 5px 10px; }
td { font-size: 11px; padding: 5px 20px; color: #000; }
tr { background: #b8d1f3; }
tr:nth-child(even) { background: #dae5f4; }
tr:nth-child(odd) { background: #b8d1f3; }
h2, th { text-align: center;font-family: Segoe UI }
</style>
"
$2 = $Computer | ConvertTo-Html -Fragment -PreContent '
<h2>Computer Info</h2>
'
ConvertTo-HTML -Head $Style -Body "$2" -Title "Contoso Computer Account Information" |  Out-File $file
Invoke-Expression C:\temp\Test.htm
menu
}

function checkpassword
{
cls
$userinfo

if (($userinfo.'Password Expiry Date') -cle (get-date)){write-host "password has expired!" -ForegroundColor White -BackgroundColor Red}
else {write-host "password has not expired" -foregroundcolor green }

if ($userinfo.lockedout){write-host "Account is currently locked out" -ForegroundColor White -BackgroundColor Red}
else {write-host "Account is not locked out" -ForegroundColor Green}

write-host " --------------------------------- "
write-host "|1. Reset password                |"
write-host "|2. Unlock Account                |"
write-host "|3. Go back to main menu          |"
write-host "|Q. Quit...                       |"
write-host " --------------------------------- "
$question = read-host "Please make a selection"

switch ($question)
        {
        "1" {password}
        "2" {unlock-account}
        "3" {cls;menu}
        "Q" {write-host "exit...";pause; exit}
        default {write-host "aborting..."; exit}}
 }

function password
{
$sam = $userinfo.SamAccountName
$gn= ($userinfo.GivenName).ToUpper()
$surname= ($userinfo.Surname).ToUpper()
$random= random -minimum 100000 -Maximum 999999
if ($userinfo.EmployeeID){$number = $userinfo.EmployeeID}
else {$number = $random}
$password = $gn.Substring(0,1) +"."+ $surname.Substring(0,1) +"_"+"$number"
Set-ADAccountPassword -Identity $sam  -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $password -Force)
$password | clip.exe
write-host -ForegroundColor Green "new password is $password and have been copied to your clipboard"
$question = read-host "Would you like to generate email? (Y/N/Q)"

    switch  ($question)
        {
        "Y" {email}
        "N" {$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof";menu}
        default {exit}
        }
}

function email
{
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.SentOnBehalfOfName = "helpdesk@Contoso.com"
$mail.to = $user.mail
$mail.Subject = "Password reset request"
$mail.Body = "Hi,

Your new password is : $password

Contoso Helpdesk Team
"
$inspector = $mail.GetInspector
$inspector.Display()
cls
$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"
menu
}

function unlock-account
{
$sam = $userinfo.SamAccountName
Unlock-ADAccount $sam

if ((get-aduser $sam -properties LockedOut).lockedout){write-host "Account still locked out, please try again" -ForegroundColor White -BackgroundColor Red}
else {write-host "Account has been unlocked" -ForegroundColor Green}

$Useraccount = get-aduser $user -ErrorAction $ErrorActionPreference –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"
menu
}

function menu
{
$userinfo= $useraccount | select-object "surname","givenname","SamAccountName","employeeid","mail","Description","AccountExpirationDate","LockedOut", @{Name="Password Expiry Date";`
Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}

write-host " --------------------------------- "
write-host "|       User lookup tool          |"
write-host " --------------------------------- "
write-host " Current user: $user"
write-host " --------------------------------- "
$userinfo
write-host " --------------------------------- "
write-host "|1. User Account information      |"
write-host "|2. Password reset                |"
write-host "|3. Computer information          |"
write-host "|4. Select another user           |"
write-host "|Q. Quit...                       |"
write-host " --------------------------------- "
$choice=read-host "Please make a selection"

switch  ($Choice)
        {
        "1" {cls;Groups}
        "2" {checkpassword}
        "3" {cls;computeraccount}
        "4" {cls;search-menu}
        "Q" {exit}
        default {menu}
        }
}

function search-menu
{
$script:Useraccount = $null
$script:userinfo =$null
cls
write-host " ------------------------------------ "
write-host "|       User lookup tool             |"
write-host " ------------------------------------ "
write-host " ------------------------------------ "
write-host "|1. Search with username             |"
write-host "|2. Search with Lastname & firstname |"
write-host "|3. search with email address        |"
write-host "|4. search with Oracle ID            |"
write-host "|Q. Quit...                          |"
write-host " ------------------------------------ "
$choice=read-host "Please make a selection"
switch  ($Choice)
        {
        "1" {$user = Read-Host "Please enter username";$filter = 'SamAccountName -like "' + $user + '"';$notfound= $user}
        "2" {$lastname = Read-Host "Please enter Lastname";$firstname = Read-Host "Please enter Firstname"
             $filter = 'surname -like "' + $lastname + '*" -and givenname -like "' + $firstname + '*"';$notfound= "$lastname, $firstname"}
        "3" {$mail= read-host "Please enter email address";$filter = 'mail -like "' + $mail + '*"';$notfound= $mail}
        "4" {$Oracleid= read-host "Please enter Oracle ID";$filter = 'employeeid -like "' + $oracleid + '"';$notfound= $Oracleid}
        "Q" {exit}
        default {exit}
        } 

$Useraccount = get-aduser -searchbase "OU=ContosoUsers,DC=ad,DC=Contoso,DC=com"`
                   –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"`
                   -Filter $filter
switch (($useraccount | measure).count)
       {
        {$_ -eq 0} {cls;write-host -ForegroundColor White -BackgroundColor Red "user $notfound not found";pause;search-menu}
        {$_ -eq 1} {$user = $Useraccount.samaccountname;cls;menu}
        {$_ -gt 1 -and $_ -cle 5} {cls;Write-Host -ForegroundColor Yellow "multiple users found with the value you entered";selectuser}
        {$_ -gt 5} {cls;write-host -ForegroundColor White -BackgroundColor Red "too much results return, abording...";pause;search-menu}
       }
}

function selectuser
{
$number = $null
foreach ($account in $Useraccount)
{
$number = 1+$number
New-Variable -Name "User$number" -Value $account.samaccountname
write-host "|$number. Sam = "$account.samaccountname" ; email ="$account.mail" ; Oracle ID = "$account.employeeid""
}
write-host ""
$choice=read-host "Please make a selection (Q to quit)"
switch  ($choice)
        {
        "1" {$user = $user1}
        "2" {$user = $user2}
        "3" {$user = $user3}
        "4" {$user = $user4}
        "5" {$user = $user5}
        "Q" {exit}
        default {exit}
        } 

$filter = 'SamAccountName -like "' + $user + '"'
$Useraccount = get-aduser -searchbase "OU=ContosoUsers,DC=ad,DC=Contoso,DC=com"`
                   –Properties "SamAccountName","Description","AccountExpirationDate","mail","LockedOut","pwdLastSet","msDS-UserPasswordExpiryTimeComputed","EmployeeID","MemberOf","givenname","memberof"`
                   -Filter $filter

switch (($useraccount | measure).count)
       {
        {$_ -eq 0} {cls;write-host -ForegroundColor White -BackgroundColor Red "issue, abording...";pause;search-menu}
        {$_ -eq 1} {$user = $Useraccount.samaccountname;cls;menu}
}
}

search-menu

Leave a comment