Search code examples
colorsforeground

Color Parts of string in Powershell


I am trying to color only the string "None Provided" versus the whole line.

So far I have:

$owned | % {write-host "Managing Group: " + ("None Found" -foreground red) "Group Description: None Provided"}

The issue is with the -foreground in the () the 'f' grays out which causes an error. It works (by coloring the whole line) without the (). Can you help me color only the "None Found"

By using Overflowed's answer it helped me fix this. Thank you!

EDIT:

if ($owned -eq $Null){
$owned | % {write-host "Managing Group: " -nonewline}
$owned | % {write-host "None Found " -foreground red -nonewline}
$owned | % {write-host "Group Description: " -nonewline}
$owned | % {write-host "None Provided " -foreground red}
}

elseif ($description -eq $Null){
$owned | % {write-host "Managing Group: "($_.name) "Group Description: " -nonewline}
$owned | % {write-host "None Provided " -foreground red}
}

else {
$owned | % {write-host "Managing Group: "($_.name) "Group Description: " ($_.description -replace "`r`n", "  ")}
}

Solution

  • Use multiple write-host with the -nonewline switch

    write-host "Managing Group: " -nonewline
    write-host "None Found" -foreground red -nonewline
    write-host "Group Description: None Provided"