{"id":162,"date":"2017-10-28T20:56:22","date_gmt":"2017-10-28T20:56:22","guid":{"rendered":"http:\/\/zewwy.ca\/?p=162"},"modified":"2023-05-21T09:43:28","modified_gmt":"2023-05-21T14:43:28","slug":"center-write-host-output","status":"publish","type":"post","link":"https:\/\/zewwy.ca\/index.php\/2017\/10\/28\/center-write-host-output\/","title":{"rendered":"Center Write-Host Output"},"content":{"rendered":"<div class=\"blogbody\">\n<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"Write-Host\"><\/span>Write-Host<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>It&#8217;s great and it&#8217;s main purposes is to well write to the host. Nothing more. So often people abuse it and leaving people to rant about it.<br \/>\nE.G. <a href=\"http:\/\/www.jsnover.com\/blog\/2013\/12\/07\/write-host-considered-harmful\/\">This guy<\/a> and the <a href=\"http:\/\/powershell-guru.com\/dont-13-use-write-host-properly-align-output\/\">&#8220;Gurus&#8221;<\/a><br \/>\nI agree with both of them in terms of displaying data, hands down. However, when it comes to simple informing the admin\/user of the script the <a href=\"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/05\/17\/writing-output-with-powershell\/\">&#8220;it displays and gets rid of that info&#8221;<\/a> is efficient, and interactive (colors).<\/p>\n<p>With that out of the way. I am working on a script to clear web parts from a sharepoint page via powershell.<br \/>\nI always like clean code an usually my scripts are interactive, for other non-interactive scripts I&#8217;d stick with Write-Output as described by the scripting guys.<br \/>\nHowever since I like to display things colorfully and neatly Write-Host is perfect!<\/p>\n<h1 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"The_Problem\"><\/span>The Problem<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Turns out there&#8217;s no easy way to get-write host to center it&#8217;s output, no mater how hard I googled. This reply from <a href=\"https:\/\/powershell.org\/forums\/topic\/center-text\/\">James Bernie<\/a> however kicked off the idea it was possible. (<a href=\"https:\/\/web.archive.org\/web\/20200918071407\/https:\/\/powershell.org\/forums\/topic\/center-text\/\">Link dead, here&#8217;s link to wayback machine<\/a>)<br \/>\nThere were actually a decent amount of issues with his propsed idea, when implemented. First we&#8217;ll want a <a href=\"https:\/\/stackoverflow.com\/questions\/38999092\/window-size-in-powershell\">static variable of the window size<\/a> at start of script.<br \/>\nThen it turns out Bernies fancy full integer trick may return whole numbers, but does so as a dang string type. Instead of wasting time dicking around with another method, i simply did <a href=\"https:\/\/stackoverflow.com\/questions\/25322636\/powershell-convert-string-to-number\">this trick of dividing by 1 on the variable<\/a>.<br \/>\nThe final problem with his concept which was driving me nuts for a good while was due to the fact of how padding method actually works.<br \/>\nPadLeft adds spaces to the left of a string.<br \/>\nThis is handy for numeric out-put because padding keeps the numbers properlly aligned on the right.<br \/>\nThis was exactly the problem I was facing, testing my existing function with a series of dots of different lengths, I found them all to be right aligned, and not centered.<br \/>\nAnother issue I found was that the pipe into measure method under an expression based section of code and calling its sub routine of count ($var | measure).count wasn&#8217;t returning the correct value.<br \/>\nThat line was pretty stupid anyway when you can simply call any variable thats of a string type length method.<br \/>\nAnd the final nail in the logical coffin, the padding was again aligning more right of center than actual center due to the fact that&#8217;s what it was comparing to first in the convert.<br \/>\nSo it made more sense to take ((Wdith of screen) &#8211; String.Length)\/2 + String.Length, this associated with a left and right padding, creates a centered master piece!!<br \/>\nFinally!!Here&#8217;s the final thing I had to overcome. My function I wanted to support Write-Host outputs color param.<br \/>\nAs it turns out, overloading functions isn&#8217;t supported in powershell, but that didn&#8217;t stop someone from comming up with a work around!<br \/>\nThis guy and his buzz works&#8230; <a href=\"http:\/\/codepyre.com\/2012\/08\/ad-hoc-polymorphism-in-powershell\/\">Woo Ad-hoc Polymorphism!!!<\/a> OK OK&#8230; here&#8217;s my final piece of code for you guys. NOTE I didn&#8217;t do fully ad hoc polythingy I cheated and only supprted foreground color via an if else.<br \/>\nIf you *burp* want to make it support background and foreground&#8230; Uhhhhh.. do it yourself&#8230; getting to wasted right now&#8230;<\/p>\n<pre>#Function to Centralize Write-Host Output, Just take string variable parameter and pads it\r\n#Nerd Level over 9000!!! Ad-hoc Polymorphic power time!!\r\n$pswwidth = (get-host).UI.RawUI.MaxWindowSize.Width\r\nfunction Centralize()\r\n{\r\n  param(\r\n  [Parameter(Position=0,Mandatory=$true)]\r\n  [string]$S,\r\n  [Parameter(Position=1,Mandatory=$false,ParameterSetName=\"color\")]\r\n  [string]$C\r\n  )\r\n    $sLength = $S.Length\r\n    $padamt =  \"{0:N0}\" -f (($pswwidth-$sLength)\/2)\r\n    $PadNum = $padamt\/1 + $sLength #the divide by one is a quick dirty trick to covert string to int\r\n    $CS = $S.PadLeft($PadNum,\" \").PadRight($PadNum,\" \") #Pad that shit\r\n    if ($C) #if variable for color exists run below\r\n    {    \r\n        Write-Host $CS -ForegroundColor $C #write that shit to host with color\r\n    }\r\n    else #need this to prevent output twice if color is provided\r\n    {\r\n        $CS #write that shit without color\r\n    }\r\n}<\/pre>\n<p>*Update* This code is being managed on GitHub, please download or fork the latest version from there, maybe one day I&#8217;ll implement background color.. :S<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write-Host It&#8217;s great and it&#8217;s main purposes is to well write to the host. Nothing more. So often people abuse it and leaving people to rant about it. E.G. This guy and the &#8220;Gurus&#8221; I agree with both of them in terms of displaying data, hands down. However, when it comes to simple informing the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zewwy.ca\/index.php\/2017\/10\/28\/center-write-host-output\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Center Write-Host Output&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","footnotes":""},"categories":[11,12,8],"tags":[],"class_list":["post-162","post","type-post","status-publish","format-standard","hentry","category-scripting","category-powershell","category-server-administration"],"_links":{"self":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":6,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":1460,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/posts\/162\/revisions\/1460"}],"wp:attachment":[{"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zewwy.ca\/index.php\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}