Question

I want to modify my ActiveDirectory lookup script to output a certain result in color.

The Script Imports the AD module and then prompts you to enter a username and see certain properties such as Name, E-mail Address and Employee ID.

There are a couple of properties whose color I'd like to change depending on the output.

For example if 'LockedOut' or 'PasswordExpired' is 'True', I would like the text color to be red for these particular results.

Is that possible? Any help is greatly appreciated!

Here is the script

Import-Module ActiveDirectory

do{ 
   $username = (read-host "Please Enter Username to Lookup")

   get-aduser $username  -properties Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber | Format-List Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber

   $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
   Clear-Host
}
while ($response -eq "Y") 
Was it helpful?

Solution

Write-Host has parameters to specify foreground and backgroundcolor:

Import-Module ActiveDirectory
[string[]]$getADProps=echo Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber
[string[]]$flProps=echo Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber
do{ 
    $username = (read-host "Please Enter Username to Lookup")
    $adUser=Get-ADUser $username  -properties $getADProps  
    if ($adUser.'LockedOut' -or $adUser.'PasswordExpired'){
        $adUser | Format-List $flProps  | Out-String | Write-Host -ForegroundColor Red
    }
    else{
        $adUser | Format-List $flProps
    }
    $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
    Clear-Host
}while ($response -eq "Y") 

OTHER TIPS

$result = get-aduser $username  -properties Created,Name,EmployeeID,EmailAddress,`
  Enabled,LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires,`
  LastLogonDate,Modified, LogonCount, HomeDirectory, Office, `
  TelephoneNumber | format-list Created,Modified, LogonCount, Name, EmailAddress,`
  EmployeeID, Enabled, LockedOut,PasswordExpired, LastLogonDate,`
  LastBadPasswordAttempt,HomeDirectory, Office,TelephoneNumber |
  out-string
write-host -foregroundcolor Red $result
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top