1

Trying to see if it is possible to change the color of the output with the below lines. Note that this is not the complete code and only part of it:

$ADInfo `
    | Format-List `
        @{ Name = "Full Name"; Expression = { $_.name } },
        @{ Name = "Display Name"; Expression = { $_.displayname } },
        @{ Name = "User ID"; Expression = { $_.samaccountname } },
        @{ Name = "Email"; Expression = { $_.mail } },
        @{ Name = "Exchange Version"; Expression = { $_.msExchVersion } },

If possible, I would like to change the color of the name and the output independently of each other.

Output:

enter image description here

5
  • 1
    Combinations of write-host -NoNewLine could help with this. Potentially with complicated logic. What are your colouring conditions? Commented Feb 25, 2015 at 21:02
  • Do you want to colour each row? Certain elements? A whole column? Would be nice to know which one. Commented Feb 25, 2015 at 21:34
  • Sorry, you do sort of have your conditions. To be sure can you make a facsimile of your output for an example? Commented Feb 25, 2015 at 21:44
  • Like @Matt said, this can be done with Write-Host and the -NoNewLine parameter. Check out my answer in this other SO question for an example of how to do it. Commented Feb 25, 2015 at 21:52
  • @Matt I added a screenshot of the output for that. I wanted to try and change the color of either column independently, if possible. So maybe change the color of the word 'Email' and the output next to it, 'sstudent' be a different color. Hope that helps clear things up. Commented Feb 26, 2015 at 0:41

1 Answer 1

2
Function Write-HostColoured{
    [cmdletbinding()]
    Param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        $Input,
        [parameter(Mandatory=$true)]
        [string]$ColouredProperty,
        [System.ConsoleColor]$Colour = "Red"

    )

    # Get all the note properties of the object which we need for output and determining conditional formatting.
    $properties = $input | gm -MemberType NoteProperty | Select-Object -ExpandProperty name

    # Find the property with the longest length. Used for aligning output. 
    $longestProperty = $properties | ForEach-Object{$_.Length} | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum

    # Check if the property to colour requested is in $properties
    If($properties -notcontains $ColouredProperty){Write-Warning "Input object does not contain the property $ColouredProperty"}

    # Insert Line break between objects. 
    Write-Host

    ForEach($singleObject in $Input){
        $properties | ForEach-Object{
            Write-Host ("{0,-$longestProperty} : " -f $_) -NoNewline

            # Check if the property requires a colour formatting
            If($_ -eq $ColouredProperty){
                Write-Host $singleObject.$_ -ForegroundColor $Colour
            } Else {
                Write-Host $singleObject.$_
            }
        }

        # Insert Line break between objects. 
        Write-Host
    }
}

Import-Csv C:\temp\data.csv -header "Col123fff1","cl2","col3","col4" | Write-HostColoured -ColouredProperty Col3

Sample Output

enter image description here

Explanation

Think this will need to evolve based on your comment but lets start with this. It will take pipeline input from an object (tested with Import-CSV) and then simulate output of Format-List. While it is outputing using Write-Host if the property being processed matches the $ColouredProperty then apply the foreground colour defined by $colour.

This currently only works if you need to colour one column but I could see this taking a hashtable input for it conditional formatting as well.

Let me know if we are on the right track.

Conditional Colouring

You could easily make the colouring conditional base on criteria if you change the if statement and add another clause

If($_ -eq $ColouredProperty -and $singleObject.$_ -match "\d")

It makes the function less transferable but it would work. I used that to highlight all file names with numbers in them.

Get-ChildItem C:\temp -filter *.txt | Select Name,Length,Directory | Write-HostColoured -ColouredProperty Name    

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

I think so. I'm still new to powershell but I was interested in coloring the output if it matched a certain string. If it wasn't too complex.
@Aaron. Figured that would be the direction you were going. That will take a little more effort but I think we can do it (was already turning the wheel inside my head about it. ). Does the column name need to be highlighted?
If it's too difficult, no. But some values I get are like.... "Account locked?: True/False"..so the reason I would like to have True or False colored is so its easier to spot.
@Aaron would you prefer the colouring to only be conditional? Or do you want to colour the row anyway? or both?
Depends how difficult it would be. I'd like to learn both to see how it works, but I might stick with the output being colored in the end.
|

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.