0

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") 

2 Answers 2

2

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") 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Dirk, unfortunately i receive the error when using your answer. Format-List : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}. At line:11 char:30 + $adUser | format-List <<<< $flProps + CategoryInfo : InvalidArgument: (:) [Format-List], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.FormatListCommand
Hi James, yes I see you are using PowerShell v2. I just tested on v3. I've made some changes to make it work on v2, too.
1
$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

2 Comments

Hi Bill, thank you for your help, it's nearly there but basically i just want the status to be a certain colour. For example when PS returns results i would like to change the colour of individual items; I would like 'Enabled' if 'True' to be green, if 'False' to be red. so that it stands out in a more clear and concise way
In order to do that, you will need to construct your own output and combine write-host -nonewline and write-host -foregroundcolor. For example: write-host -nonewline "Account disabled: " then write-host -foregroundcolor Red "True" on the next line. You won't be able to use format-table since that's going to output an object in the current console colors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.