1

It's possible to color single hashtable column in powershell?

For example color "Number" column to green:

Number VMNic
------ ----- 
[0]    vmnic0 
[1]    vmnic1 
[2]    vmnic2 
[3]    vmnic3 
[4]    vmnic4 
[5]    vmnic5 
[6]    vmnic6 
[7]    vmnic7

I create this output like that: [it's fragment from my code in the simplest way]

        $VMnics = get-vmhostnetworkadapter -Physical
        $NIC_collection = @()
        $VMnic_nr = "[$i]"
        Foreach ($VMnic in $VMnics){
                $NIC_Details = New-Object PSObject
                $NIC_Details | Add-Member -Name Number -Value $VMnic_nr -MemberType NoteProperty
                $NIC_Details | Add-Member -Name VMNic -Value $VMnic -MemberType NoteProperty
                [...]
                $NIC_collection += $NIC_Details
                $i++
             }
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize

1 Answer 1

0

You could hack it together with write-host commands, but it's not awesome. It means either manually formatting out columns, or regex matching and formatting matches. The key is that Write-Host allows you to specify colors, and it has -NoNewLine so you can string lines together.

Might there be a better way? Maybe, but I haven't found it, so if I wanted colored output to my host this is what I've done in the past.

This is pretty much what you had, but when you output the table I do it twice. The first time just lists the headers and the ---- under them. Then it skips those lines and it first converts the rest of it to strings, matches each line against the RegEx pattern "(\S+?\s+?)(\S+?\s*?)$" which means any non white-space characters, one or more, then white-space characters, one or more for the first matching group, followed by a second matching group of non white-space characters of one or more, and then any white-space characters (if any) until the end of the line. Then it writes the first matching group to the host as green, but does not create a new line, and then writes the second matching group to the host as dark yellow, and this time does put in a new line.

        $VMnics = get-vmhostnetworkadapter -Physical
        $NIC_collection = @()
        $VMnic_nr = "[$i]"
        Foreach ($VMnic in $VMnics){
                $NIC_Details = New-Object PSObject -Property @{
                    Number=$VMnic_nr
                    VMNic=$VMnic
                }
                #[...]
                $NIC_collection += $NIC_Details
                $i++
             }
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize | Select -First 3
        $NIC_collection | Sort-Object VMNic | ft * -AutoSize | Select -Skip 3 | Out-String | ?{$_ -match "(\S+?\s+?)(\S+?\s*?)$"} | %{Write-Host $Matches[1] -ForegroundColor Green -NoNewline;Write-Host $Matches[2] -ForegroundColor DarkYellow}
Sign up to request clarification or add additional context in comments.

Comments

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.