{"id":3462,"date":"2013-09-27T10:11:20","date_gmt":"2013-09-27T14:11:20","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=3462"},"modified":"2015-10-23T12:57:39","modified_gmt":"2015-10-23T16:57:39","slug":"friday-fun-out-conditionalcolor","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/","title":{"rendered":"Friday Fun: Out-ConditionalColor"},"content":{"rendered":"<p>Last week I posted a <a title=\"Readh Friday Fun Color My Web\" href=\"http:\/\/jdhitsolutions.com\/blog\/2013\/09\/friday-fun-color-my-web\/\" target=\"_blank\">Friday Fun article<\/a> on parsing results from Invoke-Webrequest and displaying matching strings in color so that the book titles I'm interested in stand out. But the more I thought about it I realized I should take this a step further. The problem with Write-Host is that it doesn't write anything to the pipeline which is why we typically frown upon its use. Although being able to use color to highlight something is very cool. I wanted color and pipelined output. Here's my result.<\/p>\n<pre class=\"lang:ps decode:true \">#requires -version 3.0\r\n\r\nFunction Out-ConditionalColor {\r\n&lt;#\r\n.Synopsis\r\nDisplay colorized pipelined output.\r\n.Description\r\nThis command is designed to take pipeline input and display it in a colorized\r\nformat, based on a set of conditions. Unlike Write-Host which doesn't write to\r\nthe pipeline, this command will write to the pipeline. You can get colorized \r\ndata and save the output to a variable at the same time, although you'll need\r\nto use the common OutVariable parameter (see examples).\r\n\r\nThe default behavior is to use a hash table with a property name and color. \r\nThe color must be one of the standard console colors used with Write-Host.\r\n\r\n$c = @{Stopped='Red';Running='Green'}\r\n\r\nYou can then pipe an expression to this command, specifying a property name and\r\nthe hash table. If the property matches the key name, the output for that object\r\nwill be colored using the corresponding hash table value.\r\n\r\nget-service -diplayname windows* | out-conditionalcolor $c status \r\n\r\nOr you can do more complex processing with an ordered hash table constructed \r\nusing this format:\r\n\r\n[ordered]@{ &lt;comparison scriptblock&gt; = &lt;color&gt;}\r\n\r\nThe comparison scriptblock can use $PSitem.\r\n\r\n$h=[ordered]@{\r\n{$psitem.ws -gt 200mb}='yellow'\r\n{$psitem.vm -gt 500mb}='red'\r\n{$psitem.cpu -gt 300}='cyan'\r\n}\r\n\r\nWhen doing a complex comparison you must use an [ordered] hashtable as each key \r\nwill be processed in order using an If\/ElseIf statement.\r\n\r\nThis command should be the last part of any pipelined expression. If you pipe\r\nto anything else, such as Sort-Object, you will lose your color formatting. Do\r\nany other sorting or filtering before piping to this command.\r\n\r\nThis command requires PowerShell 3.0 and works best in the PowerShell console.\r\n.Parameter Conditions\r\nUse a simple hashtable for basic processing or an ordered hash table for complex.\r\n.Parameter Property\r\nWhen using a simple hash table, specify the property to compare using the -eq\r\noperator. If you don't specify a property you will be prompted.\r\n.Example\r\nPS C:\\&gt; get-service -displayname windows* | out-conditionalcolor -conditions @{Stopped='Red'} -property Status\r\n\r\nGet all services where the displayname starts with windows and display stopped services in red.\r\n.Example\r\nPS C:\\&gt; get-service -displayname windows* | out-conditionalcolor @{Stopped='Red'} status -ov winstop\r\n\r\nRepeat the previous example, but save the output to the variable winstop. When\r\nyou look at $Winstop you'll see the services, but they won't be colored. This \r\nexample uses the parameters positionally.\r\n.Example\r\nPS C:\\&gt; get-eventlog system -newest 50 | out-conditionalcolor @{error='red';warning='yellow'}\r\nEnter a property name: entrytype\r\n\r\nGet the newest 50 entries from the System event log. Display errors in red and warnings in yellow.\r\nIf you don't specify a property you will be prompted.\r\n.Example\r\nPS C:\\&gt; $c =[ordered]@{{$psitem.length -ge 1mb}='red';{$psitem.length -ge 500KB}='yellow';{$psitem.length -ge 100KB}='cyan'}\r\nPS C:\\&gt; dir c:\\scripts\\*.doc,c:\\scripts\\*.pdf,c:\\scripts\\*.xml | Select Name,LastWriteTime,Length | out-conditionalcolor $c\r\n\r\nThe first command creates an ordered hashtable based on the Length property. \r\nThe second command uses it to et certain file types in the scripts folder and \r\ndisplay the selected properties in color depending on the file size.\r\n.Notes\r\nLast Updated: 9\/17\/2013\r\nVersion     : 0.9\r\n\r\nLearn more about PowerShell:\r\n<blockquote class=\"wp-embedded-content\" data-secret=\"cpNUTiPRjv\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/essential-powershell-resources\/\">Essential PowerShell Learning Resources<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Essential PowerShell Learning Resources&#8221; &#8212; The Lonely Administrator\" src=\"https:\/\/jdhitsolutions.com\/blog\/essential-powershell-resources\/embed\/#?secret=D6eJh62eew#?secret=cpNUTiPRjv\" data-secret=\"cpNUTiPRjv\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\r\n\r\n  ****************************************************************\r\n  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *\r\n  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *\r\n  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *\r\n  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *\r\n  ****************************************************************\r\n\r\n.Link\r\n<blockquote class=\"wp-embedded-content\" data-secret=\"ioa2eIylLW\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/\">Friday Fun: Out-ConditionalColor<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Friday Fun: Out-ConditionalColor&#8221; &#8212; The Lonely Administrator\" src=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/embed\/#?secret=SWqfHjHo14#?secret=ioa2eIylLW\" data-secret=\"ioa2eIylLW\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\r\n.Link\r\nAbout_Hash_Tables\r\n#&gt;\r\n\r\n[cmdletbinding()]\r\nParam(\r\n[Parameter(Position=0,Mandatory=$True,HelpMessage=\"Enter an ordered hashtable of conditional properties and colors.\")]\r\n[ValidateScript({ \r\n$_ -IS [System.Collections.Specialized.OrderedDictionary] -OR ($_ -IS [hashtable])})]\r\n[psobject]$Conditions,\r\n[Parameter(Position=1,HelpMessage=\"Enter a property name.\")]\r\n[string]$Property,\r\n[Parameter(Position=2,Mandatory=$True,ValueFromPipeline=$True)]\r\n[PSObject[]]$Inputobject\r\n\r\n)\r\n\r\nBegin {\r\n    Write-Verbose \"Starting $($MyInvocation.MyCommand)\"\r\n    #save original color\r\n    $saved = $Host.UI.RawUI.ForegroundColor\r\n    Write-Verbose \"Original foreground color is $saved\"\r\n\r\n    #validate colors\r\n    $allowed = [enum]::GetNames([system.consolecolor])\r\n    $bad = $Conditions.Values | where {$allowed -notcontains $_}\r\n    if ($bad) {\r\n        Write-Warning \"You are using one or more invalid colors: $($bad -join ',')\"\r\n        Break\r\n    }    \r\n   \r\n    if ($Conditions -is [System.Collections.Specialized.OrderedDictionary]) {\r\n        $Complex = $True\r\n        #we'll need this later in the Process script block\r\n        #if doing complex processing\r\n        Write-Verbose \"Getting hash table enumerator and names\"\r\n        $compare = $conditions.GetEnumerator().name\r\n        Write-Verbose $($compare | out-string)\r\n        #build an If\/ElseIf construct on the fly\r\n#the Here strings must be left justified\r\n$If=@\"\r\n if ($($compare[0])) {\r\n  `$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[0])))'\r\n }\r\n\"@\r\n\r\n        #now add the remaining comparisons as ElseIf\r\n        for ($i=1;$i -lt $conditions.count;$i++) {\r\n         $If+=\"elseif ($($compare[$i])) { \r\n         `$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[$i])))'\r\n         }\r\n         \"\r\n        } #for\r\n\r\n#add the final else\r\n$if+=@\"\r\nElse { \r\n`$host.ui.RawUI.ForegroundColor = `$saved \r\n}\r\n\"@\r\n\r\n        Write-Verbose \"Complex comparison:\"\r\n        Write-Verbose $If\r\n    } #if complex parameter set\r\n    Else {\r\n        #validate a property was specified\r\n        if (-NOT $Property) {\r\n            [string]$property = Read-Host \"Enter a property name\"\r\n            if (-Not $property) { \r\n                Write-Verbose \"Blank property so quitting\"\r\n                Break\r\n            }\r\n        }\r\n        Write-Verbose \"Conditional property is $Property\"\r\n    } \r\n\r\n} #Begin\r\n\r\nProcess {\r\n\r\n    If ($Complex) {\r\n        #Use complex processing\r\n        Invoke-Expression $if        \r\n  } #end complex\r\n  else {\r\n        #get property value as a string\r\n        $value = $Inputobject.$Property.ToString()\r\n        Write-Debug \"Testing property value $value\"\r\n        if ($Conditions.containsKey($value)) {\r\n         Write-Debug \"Property match\"\r\n         $host.ui.RawUI.ForegroundColor= $Conditions.item($value)\r\n        }\r\n        else {\r\n         #use orginal color\r\n         Write-Debug \"No matches found\"\r\n         $host.ui.RawUI.ForegroundColor= $saved\r\n        }        \r\n  } #simple\r\n\r\n    #write the object to the pipeline\r\n    Write-Debug \"Write the object to the pipeline\"\r\n    $_\r\n} #Process\r\n\r\nEnd {\r\n    Write-Verbose \"Restoring original foreground color\"\r\n    #set color back\r\n    $Host.UI.RawUI.ForegroundColor=$saved\r\n} #end\r\n\r\n} #close function\r\n\r\n#create an optional alias\r\nSet-Alias -Name occ -Value Out-ConditionalColor<\/pre>\n<p>This function only really works in the PowerShell console. It is designed to accept any PowerShell input. The function also takes parameters that help it write matching objects in the specified color. The easy way to use the function is to specify a property and a hashtable. The hashtable key is the value you want to match in the input and the key is the color you want to use. The function processes each object as it comes in and if the property matches the value it sets the foreground color of the console to the specified value.<\/p>\n<pre class=\"lang:ps decode:true \">$value = $Inputobject.$Property.ToString()\r\n        Write-Debug \"Testing property value $value\"\r\n        if ($Conditions.containsKey($value)) {\r\n         Write-Debug \"Property match\"\r\n         $host.ui.RawUI.ForegroundColor= $Conditions.item($value)\r\n        }<\/pre>\n<p>If it doesn't match then the foreground color essentially remains the same. This means I can run a command like this:<\/p>\n<pre class=\"lang:ps decode:true \">get-process | sort WS -descending | Out-ConditionalColor @{chrome='yellow'} -Property Name<\/pre>\n<p>And get a result like this:<br \/>\n<a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3463\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png\" alt=\"occ-basic\" width=\"625\" height=\"389\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-300x187.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-624x389.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic.png 1137w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Any time the process name is equal to 'chrome' I display that object in yellow. You can also have multiple entries.<\/p>\n<pre class=\"lang:ps decode:true \">get-eventlog system -newest 50 | out-conditionalcolor @{error='red';warning='yellow'}<\/pre>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3464\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2-1024x476.png\" alt=\"occ-basic2\" width=\"625\" height=\"290\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2-1024x476.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2-300x139.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2-624x290.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic2.png 1137w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\nI mentioned match earlier but it is really a simple equality test. However, I also added code so that you could be even more granular.<br \/>\nYou can also create what I refer to as a complex hashtable. This must be an ordered hashtable, which means you must be running PowerShell 3.0. Define a hashtable like this:<\/p>\n<pre class=\"lang:ps decode:true \">$h=[ordered]@{\r\n{$psitem.ws -gt 200mb}='yellow'\r\n{$psitem.vm -gt 500mb}='red'\r\n{$psitem.cpu -gt 300}='cyan'\r\n}<\/pre>\n<p>This is a little tricky. The hashtable key is a scriptblock of the object property you want to watch and some operator comparison. The value is the console color you want to use for the match. Use $psitem to represent the current object in the pipeline. The reason for the ordered hashtable is because the keys are processed in an If\/ElseIf fashion so make sure you get things in the right order.<\/p>\n<p>The function breaks apart the hashtable and builds a here string of the If\/ElseIf structure in the Begin scriptblock.<\/p>\n<pre class=\"lang:ps decode:true \">    if ($Conditions -is [System.Collections.Specialized.OrderedDictionary]) {\r\n        $Complex = $True\r\n        #we'll need this later in the Process script block\r\n        #if doing complex processing\r\n        Write-Verbose \"Getting hash table enumerator and names\"\r\n        $compare = $conditions.GetEnumerator().name\r\n        Write-Verbose $($compare | out-string)\r\n        #build an If\/ElseIf construct on the fly\r\n#the Here strings must be left justified\r\n$If=@\"\r\n if ($($compare[0])) {\r\n  `$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[0])))'\r\n }\r\n\"@\r\n\r\n        #now add the remaining comparisons as ElseIf\r\n        for ($i=1;$i -lt $conditions.count;$i++) {\r\n         $If+=\"elseif ($($compare[$i])) { \r\n         `$host.ui.RawUI.ForegroundColor = '$($conditions.item($($compare[$i])))'\r\n         }\r\n         \"\r\n        } #for\r\n\r\n#add the final else\r\n$if+=@\"\r\nElse { \r\n`$host.ui.RawUI.ForegroundColor = `$saved \r\n}\r\n\"@\r\n\r\n        Write-Verbose \"Complex comparison:\"\r\n        Write-Verbose $If\r\n    } #if complex parameter set<\/pre>\n<p>The here string includes the commands to set the host foreground color. Then in the Process scriptblock I use Invoke-Expression to execute it.<\/p>\n<pre class=\"lang:ps decode:true \">   If ($Complex) {\r\n        #Use complex processing\r\n        Invoke-Expression $if\r\n  } #end complex<\/pre>\n<p>But now I can do this.<\/p>\n<pre class=\"lang:ps decode:true \">get-process | out-conditionalcolor -Conditions $h<\/pre>\n<p><a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3466\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex-1024x638.png\" alt=\"occ-complex\" width=\"625\" height=\"389\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex-1024x638.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex-300x187.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex-624x389.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-complex.png 1137w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\nWhen using a complex hashtable, there's no need to specify a property. What's great about all of this is that if you want to see the color output and still save the results, use the common -Outvariable parameter with Out-Conditionalcolor.<\/p>\n<pre class=\"lang:ps decode:true \">get-process | out-conditionalcolor -Conditions $h -OutVariable p<\/pre>\n<p>$P won't be colorized (unless I run it through the function again), but I still have the data.<\/p>\n<p>There are some limitations. Like any Out cmdlet, this must be the last command in your pipelined expression. Technically you could sort or filter after my function, but it will lose the conditional coloring. Unlike the other Out cmdlets, you cannot piped any formatted data to it.<\/p>\n<p>The whole point of the function is to provide a means of adding some visual references to your PowerShell data.<br \/>\n<a href=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3467\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3-1024x638.png\" alt=\"occ-basic3\" width=\"625\" height=\"389\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3-1024x638.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3-300x187.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3-624x389.png 624w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic3.png 1137w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a>I sincerely hope you'll try this out and let me know what you think.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week I posted a Friday Fun article on parsing results from Invoke-Webrequest and displaying matching strings in color so that the book titles I&#8217;m interested in stand out. But the more I thought about it I realized I should take this a step further. The problem with Write-Host is that it doesn&#8217;t write anything&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New blog post - Friday Fun: Out-ConditionalColor","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[271,4,359,8],"tags":[568,32,199,298,534,540],"class_list":["post-3462","post","type-post","status-publish","format-standard","hentry","category-friday-fun","category-powershell","category-powershell-3-0","category-scripting","tag-friday-fun","tag-functions","tag-hashtable","tag-invoke-expression","tag-powershell","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"Last week I posted a Friday Fun article on parsing results from Invoke-Webrequest and displaying matching strings in color so that the book titles I&#039;m interested in stand out. But the more I thought about it I realized I should take this a step further. The problem with Write-Host is that it doesn&#039;t write anything...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2013-09-27T14:11:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-10-23T16:57:39+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Friday Fun: Out-ConditionalColor\",\"datePublished\":\"2013-09-27T14:11:20+00:00\",\"dateModified\":\"2015-10-23T16:57:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/\"},\"wordCount\":561,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/occ-basic-1024x638.png\",\"keywords\":[\"Friday Fun\",\"functions\",\"hashtable\",\"Invoke-Expression\",\"PowerShell\",\"Scripting\"],\"articleSection\":[\"Friday Fun\",\"PowerShell\",\"Powershell 3.0\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/\",\"name\":\"Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/occ-basic-1024x638.png\",\"datePublished\":\"2013-09-27T14:11:20+00:00\",\"dateModified\":\"2015-10-23T16:57:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/occ-basic.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/09\\\/occ-basic.png\",\"width\":1137,\"height\":709},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/3462\\\/friday-fun-out-conditionalcolor\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Friday Fun\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/friday-fun\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Friday Fun: Out-ConditionalColor\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/","og_locale":"en_US","og_type":"article","og_title":"Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator","og_description":"Last week I posted a Friday Fun article on parsing results from Invoke-Webrequest and displaying matching strings in color so that the book titles I'm interested in stand out. But the more I thought about it I realized I should take this a step further. The problem with Write-Host is that it doesn't write anything...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/","og_site_name":"The Lonely Administrator","article_published_time":"2013-09-27T14:11:20+00:00","article_modified_time":"2015-10-23T16:57:39+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Friday Fun: Out-ConditionalColor","datePublished":"2013-09-27T14:11:20+00:00","dateModified":"2015-10-23T16:57:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/"},"wordCount":561,"commentCount":0,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png","keywords":["Friday Fun","functions","hashtable","Invoke-Expression","PowerShell","Scripting"],"articleSection":["Friday Fun","PowerShell","Powershell 3.0","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/","name":"Friday Fun: Out-ConditionalColor &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic-1024x638.png","datePublished":"2013-09-27T14:11:20+00:00","dateModified":"2015-10-23T16:57:39+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/occ-basic.png","width":1137,"height":709},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3462\/friday-fun-out-conditionalcolor\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Friday Fun","item":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},{"@type":"ListItem","position":2,"name":"Friday Fun: Out-ConditionalColor"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":904,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/904\/friday-fun-color-my-world\/","url_meta":{"origin":3462,"position":0},"title":"Friday Fun: Color My World","author":"Jeffery Hicks","date":"September 3, 2010","format":false,"excerpt":"The end of another work week and time for a little PowerShell fun. When I first started using PowerShell, I was fascinated by Write-Host and the ability to write colorized text to the console. Visions of ANSI art danced in my head, but I've moved on. Using colors with Write-Host\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/09\/color-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/09\/color-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/09\/color-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3455,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3455\/friday-fun-color-my-web\/","url_meta":{"origin":3462,"position":1},"title":"Friday Fun Color My Web","author":"Jeffery Hicks","date":"September 20, 2013","format":false,"excerpt":"Awhile ago I posted an article demonstrating using Invoke-Webrequest which is part of PowerShell 3.0. I used the page at Manning.com to display the Print and MEAP bestsellers. By the way, thanks to all of you who keep making PowerShell books popular. My original script simply wrote the results to\u2026","rel":"","context":"In &quot;Books&quot;","block_context":{"text":"Books","link":"https:\/\/jdhitsolutions.com\/blog\/category\/books\/"},"img":{"alt_text":"get-manningbestseller1","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-manningbestseller1-1024x606.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-manningbestseller1-1024x606.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-manningbestseller1-1024x606.png?resize=525%2C300 1.5x"},"classes":[]},{"id":840,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/840\/pipelines-consoles-and-hosts\/","url_meta":{"origin":3462,"position":2},"title":"Pipelines, Consoles and Hosts","author":"Jeffery Hicks","date":"August 19, 2010","format":false,"excerpt":"I continue to come across a particular topic in discussion forums that causes many PowerShell beginners a lot of headaches and more than a little frustration. I know I've written about this before and I'm sure I'll cover it again, but when writing anything in PowerShell that you see in\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2010\/08\/write-demo.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1069,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/1069\/friday-fun-out-rainbow\/","url_meta":{"origin":3462,"position":3},"title":"Friday Fun Out-Rainbow","author":"Jeffery Hicks","date":"January 21, 2011","format":false,"excerpt":"For my readers who are just discovering my Friday posts, let me remind you that these are not necessarily practical, production worthy PowerShell scripts and functions. They are meant to be fun, yet educational. For example, in today's Friday Fun I have a function that takes string input and writes\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/01\/out-rainbow-1-1024x412.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/01\/out-rainbow-1-1024x412.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/01\/out-rainbow-1-1024x412.png?resize=525%2C300 1.5x"},"classes":[]},{"id":445,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/445\/more-fun-with-get-numberedcontent\/","url_meta":{"origin":3462,"position":4},"title":"More Fun with Get-NumberedContent","author":"Jeffery Hicks","date":"October 13, 2009","format":false,"excerpt":"As much fun as the original Get-NumberedContent function was after using it for awhile I realized I had imposed some limitations. I also realized it needed to be more flexible. What if someone wanted to specify a different color or use a different comment character such as a ; in\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"Help Get-NumberedContent","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2009\/10\/captured_Image2.png_thumb2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3140,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3140\/friday-fun-quote-of-the-day-revised\/","url_meta":{"origin":3462,"position":5},"title":"Friday Fun: Quote of the Day Revised","author":"Jeffery Hicks","date":"June 28, 2013","format":false,"excerpt":"This week TrainSignal has been running a contest to celebrate my new PowerShell 3.0 course . All you have to do to win is enter some off-the-wall, silly or non-production use of PowerShell. I've posted a few examples on the TrainSignal blog this week. \u00a0These Friday Fun posts I write\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"talkbubble-v3","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/talkbubble-v3-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3462"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3462\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}