PowerShell Script to Retrieve Basic User Stats

This is a quick and dirty PowerShell script to retrieve the user statistics that I need most frequently. It works great for my help desk too.

# Filename : get_status.ps1
# Purpose : Queries basic account status info.
#
# Requires the following applications to be installed:
# Quest ActiveRoles AD Management Snapin
#
# Modified : 2013.03.05 Jay Carper; Created
#

Param([string]$Username)

# If a username wasn’t specified, prints syntax and exits.
if (($Username -eq $Null) -or ($Username -eq “”)) {
[console]::ForegroundColor = “white”
write-host ” “
write-host “Syntax: Get_Status “
write-host ” “
write-host “The username can be in the form of an alias, primary SMTP`
address, domain\username, or UPN.”
write-host ” “
[console]::ForegroundColor = “gray”
break
}

# Loads the Quest ActiveRoles AD Management snapin if it isn’t already loaded.
if ((get-pssnapin -name quest.activeroles.admanagement -erroraction silentlycontinue) -eq $null) {
add-pssnapin quest.activeroles.admanagement
}

# Sends data to the screen.
if (get-qaduser $username) {
[console]::ForegroundColor = “white”
get-qaduser $username | fl SamAccountName, PrimarySMTPAddress, PasswordStatus,`
LastLogonTimeStamp, LastLogon, AccountIsLockedOut, AccountIsExpired
write-host “LastLogonTimeStamp is replicated between domain controllers”
write-host “more slowly than other properties and can be up to 7 days off.”
write-host ” “
write-host ” “
[console]::ForegroundColor = “gray”
}
else {
[console]::ForegroundColor = “red”
write-host ” “
write-host “User $username not found.”
write-host ” “
write-host ” “
[console]::ForegroundColor = “gray”
}

Copy this script into Notepad, save it as get_status.ps1, and then copy it to “C:\Windows\System32\WindowsPowerShell\v1.0”.

The output will look like this:

[PS] C:\Windows\system32>get_status.ps1 jay.test

SamAccountName : jay.test
PrimarySMTPAddress : Jay.Test@domain.com
PasswordStatus : Expires at: Monday, August 12, 2013
LastLogonTimestamp : 2/28/2013 8:02:42 AM
LastLogon : 3/5/2013 8:08:47 AM
AccountIsLockedOut : False
AccountIsExpired : False

LastLogonTimeStamp is replicated between domain controllers
more slowly than other properties and can be up to 7 days off.

[PS] C:\Windows\system32>

 

Caution: WordPress seems to want to change the line feeds in the scripts I post without warning or apparent pattern.

Leave a Reply

Your email address will not be published.