{"id":223,"date":"2019-02-14T03:14:50","date_gmt":"2019-02-14T08:14:50","guid":{"rendered":"http:\/\/itblog.ldlnet.net\/?p=223"},"modified":"2019-02-14T03:14:50","modified_gmt":"2019-02-14T08:14:50","slug":"customize-your-default-powershell-cli-prompt","status":"publish","type":"post","link":"https:\/\/itblog.ldlnet.net\/index.php\/2019\/02\/14\/customize-your-default-powershell-cli-prompt\/","title":{"rendered":"Customize your Default PowerShell CLI Prompt"},"content":{"rendered":"\n<p>We all like to have our customization in our Windows Desktop. Custom colors, icons, wallpaper, etc.. Well IT guy\/gal, why not have your PowerShell CLI the same way? I&#8217;ve looked around at a few blogs and got some ideas to share with you on customizing your PowerShell CLI.<\/p>\n\n\n\n<p>Now, by default, Windows looks in the following directory for your customization file:<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-purple-color\"><strong>C:\\Users\\(username)\\Documents\\WindowsPowerShell <\/strong><\/p>\n\n\n\n<p>It looks for a file called profile.ps1 and will load that script every time you load PowerShell once it is customized.<\/p>\n\n\n\n<p>You can construct the script within PowerShell ISE or your favorite editor. The following is how I programmed the script to customize my PowerShell prompt:<\/p>\n\n\n\n<p style=\"text-align:center\" class=\"has-small-font-size\"><strong>First,\u00a0we\u00a0want\u00a0to\u00a0clear\u00a0the\u00a0slate\u00a0on\u00a0the\u00a0PowerShell\u00a0CLI.\u00a0<\/strong><br><strong>I\u00a0like\u00a0to\u00a0add\u00a0my\u00a0contact\u00a0information\u00a0for\u00a0instance.<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">#Set the PowerShell CLI for customization. \n#Written by Lance Lingerfelt \/ LDLNET LLC - lance.lingerfelt@ldlnet.net - http:\/\/www.ldlnet.net\n\n#Start Script#\n\n#Clear the default PowerShell Window\nClear-Host<\/pre>\n\n\n\n<p style=\"text-align:center\" class=\"has-small-font-size\"><strong>Second, I run a script that I came across and added to my customization.<br>It get&#8217;s me the weather for the local area that I am from.\u00a0Click\u00a0the\u00a0link\u00a0for\u00a0details.<\/strong><br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"How To Uniquify Your PowerShell Console (Scroll to Getting the Weather) (opens in a new tab)\" href=\"https:\/\/dev.to\/hf-solutions\/how-to-uniquify-your-powershell-profile-2b35\" target=\"_blank\">How To Uniquify Your PowerShell Console (Scroll to Getting the Weather)<\/a><\/em><\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">#Run the Script Get-Weather.ps1 to get the weather forcast.\n. $PSScriptRoot\\Get-Weather.ps1\nGet-Weather -City 'Charlotte' -Country 'USA'<\/pre>\n\n\n\n<p style=\"text-align:center\" class=\"has-small-font-size\"><strong>Next,\u00a0I\u00a0customize\u00a0the\u00a0PowerShell\u00a0Window\u00a0Settings\u00a0so\u00a0that\u00a0it\u00a0is\u00a0the\u00a0size\u00a0and\u00a0shape\u00a0that\u00a0I\u00a0want.<\/strong><br><strong>I\u00a0set\u00a0the\u00a0Directory\u00a0Location,\u00a0The\u00a0Colors,\u00a0and\u00a0The\u00a0Window\u00a0Sizes.<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">#Customize the PowerShell Window Settings\nset-location c:\\PowerShell\n$a = (Get-Host).UI.RawUI\n$a.BackgroundColor = \"black\"\n$a.ForegroundColor = \"white\"\n$size = $a.WindowSize\n$size.Width = 200\n$size.Height = 50\n$a.WindowSize = $size\n$size = $a.BufferSize\n$size.Width = 200\n$size.Height = 9999\n$a.BufferSize = $size<\/pre>\n\n\n\n<p style=\"text-align:center\" class=\"has-small-font-size\"><strong>Next, I wanted to write some text in the window before I get my PowerShell Prompt.<\/strong><br><strong>I work at <a rel=\"noreferrer noopener\" aria-label=\"Avanade (opens in a new tab)\" href=\"http:\/\/www.avanade.com\" target=\"_blank\">Avanade<\/a>, so I wanted to put a welcome message, today&#8217;s date, and what PS version I was running just because I could.<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">#Customize the text to be shown before the prompt\n$time = Get-Date\n$curUser= (Get-ChildItem Env:\\USERNAME).Value\n$psVersion= $host.Version.Major\nWrite-Host \"Welcome to Avanade PowerShell, $curUser!\" -foregroundColor Green\nWrite-Host \"Today is: $($time.ToLongDateString())\" -foregroundColor Yellow\nWrite-Host \"You're running PowerShell version: $psVersion\" -foregroundColor Red `n <\/pre>\n\n\n\n<p style=\"text-align:center\" class=\"has-small-font-size\"><strong>Lastly, we actually configure the prompt. There is a lot of ways to do this and I will leave references for you at the bottom of this post so that you can get more details on the commands actually run.<\/strong><br><strong>I wanted to have a colorful prompt that stated the company motto and put the current time.<\/strong><br><strong>I also configured the Window Title at the top to show text, the current user, and the current directory with different colors as that is my expressive part of my brain. \ud83d\ude42<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">#Customize the actual PS prompt\nfunction prompt {\n\nWrite-Host -NoNewLine \"Avanade... \" -foregroundColor Red\nWrite-Host -NoNewLine \"Know, Share, Do: \" -foregroundColor White\nWrite-Host -NoNewLine \"[\" -foregroundColor Yellow\nWrite-Host -NoNewLine (Get-Date -f \"hh:mm:ss tt\") -foregroundColor Cyan\nWrite-Host -NoNewLine \"]\" -foregroundColor Yellow\nWrite-Host -NoNewLine \">\" -foregroundColor Red\n$host.UI.RawUI.WindowTitle = \"Avanade PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)\"\n\nReturn \" \"\n\n}\n\n#End Script#<\/pre>\n\n\n\n<p><strong>Here is the script in its entirety:<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell\" title=\"profile.ps1\">#Set the PowerShell CLI for customization. \n#Written by Lance Lingerfelt \/ LDLNET LLC - lance.lingerfelt@ldlnet.net - http:\/\/www.ldlnet.net\n\n#Start Script#\n\n#Clear the default PowerShell Window\nClear-Host\n\n#Run the Script Get-Weather.ps1 to get the weather forcast.\n. $PSScriptRoot\\Get-Weather.ps1\nGet-Weather -City 'Charlotte' -Country 'USA'\n\n#Customize the PowerShell Window Settings\nset-location c:\\PowerShell\n$a = (Get-Host).UI.RawUI\n$a.BackgroundColor = \"black\"\n$a.ForegroundColor = \"white\"\n$size = $a.WindowSize\n$size.Width = 200\n$size.Height = 50\n$a.WindowSize = $size\n$size = $a.BufferSize\n$size.Width = 200\n$size.Height = 9999\n$a.BufferSize = $size\n\n#Customize the text to be shown before the prompt\n$time = Get-Date\n$curUser= (Get-ChildItem Env:\\USERNAME).Value\n$psVersion= $host.Version.Major\nWrite-Host \"Welcome to Avanade PowerShell, $curUser!\" -foregroundColor Green\nWrite-Host \"Today is: $($time.ToLongDateString())\" -foregroundColor Yellow\nWrite-Host \"You're running PowerShell version: $psVersion\" -foregroundColor Red `n\n\n#Customize the actual PS prompt\nfunction prompt {\n\nWrite-Host -NoNewLine \"Avanade... \" -foregroundColor Red\nWrite-Host -NoNewLine \"Know, Share, Do: \" -foregroundColor White\nWrite-Host -NoNewLine \"[\" -foregroundColor Yellow\nWrite-Host -NoNewLine (Get-Date -f \"hh:mm:ss tt\") -foregroundColor Cyan\nWrite-Host -NoNewLine \"]\" -foregroundColor Yellow\nWrite-Host -NoNewLine \">\" -foregroundColor Red\n$host.UI.RawUI.WindowTitle = \"Avanade PowerShell >> User: $curUser >> Current Directory: $((Get-Location).Path)\"\n\nReturn \" \"\n\n}\n\n#End Script#\n<\/pre>\n\n\n\n<p>Here is the final product when opening PowerShell:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/02\/Avanade-Custom-PowerShell-Prompt-1024x304.png\" alt=\"Avanade Custom PowerShell Prompt\" class=\"wp-image-226\" width=\"756\" height=\"224\" srcset=\"https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/02\/Avanade-Custom-PowerShell-Prompt-1024x304.png 1024w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/02\/Avanade-Custom-PowerShell-Prompt-300x89.png 300w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/02\/Avanade-Custom-PowerShell-Prompt-768x228.png 768w, https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2019\/02\/Avanade-Custom-PowerShell-Prompt.png 1089w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><figcaption>My Customized PS Prompt<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"text-align:center\">HAPPY SCRIPTING!<br>HAPPY VALENTINES DAY!<\/h2>\n\n\n\n<p class=\"has-small-font-size\"><strong>References:<\/strong><br><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.howtogeek.com\/50236\/customizing-your-powershell-profile\/\" target=\"_blank\"><em><strong>Customizing your PowerShell Profile<\/strong><\/em><\/a><br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"Get-Weather.ps1 (opens in a new tab)\" href=\"https:\/\/gist.github.com\/Alcha\/f08a83486f7d064be9d4f73300a33872\" target=\"_blank\">Get-Weather.ps1<\/a><\/em><\/strong><br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"Modify your PowerShell Prompt (opens in a new tab)\" href=\"http:\/\/akuederle.com\/modify-your-powershell-prompt\" target=\"_blank\">Modify your PowerShell Prompt<\/a><\/em><\/strong><br><strong><em><a rel=\"noreferrer noopener\" aria-label=\"PowerShell Basics: Console Configuration (opens in a new tab)\" href=\"https:\/\/www.itprotoday.com\/powershell\/powershell-basics-console-configuration\" target=\"_blank\">PowerShell Basics: Console Configuration<\/a><\/em><\/strong><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all like to have our customization in our Windows Desktop. Custom colors, icons, wallpaper, etc.. Well IT guy\/gal, why not have<\/p>\n<p class=\"link-more\"><a class=\"myButt \" href=\"https:\/\/itblog.ldlnet.net\/index.php\/2019\/02\/14\/customize-your-default-powershell-cli-prompt\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,16],"tags":[102,15,8,103],"class_list":["post-223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","category-powershell","category-windows","tag-cli","tag-cmdlet","tag-powershell","tag-ps-profile","odd"],"_links":{"self":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":3,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/223\/revisions\/229"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media\/147"}],"wp:attachment":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}