{"id":119,"date":"2014-12-28T00:01:00","date_gmt":"2014-12-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/12\/28\/weekend-scripter-playing-with-powershell-prompt\/"},"modified":"2019-02-18T10:36:19","modified_gmt":"2019-02-18T17:36:19","slug":"weekend-scripter-playing-with-powershell-prompt","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-playing-with-powershell-prompt\/","title":{"rendered":"Weekend Scripter: Playing with PowerShell Prompt"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about playing with the Windows PowerShell prompt.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is time to round up all of those end-of-the-year kinds of things. This is, in fact, the last weekend Hey, Scripting Guy! Blog post of 2014. Not sure what that means; but it is true, this is the last weekend post of the year. Not that I am going to do anything special, or make a big deal of it, but I just was thinking about it.<\/p>\n<p>One thing I like to do from time-to-time, is override my Windows PowerShell prompt. The cool thing is that I can do it on the fly. This is because it is simply a function&mdash;and like all functions, I can overwrite the behavior.<\/p>\n<p>Like all other Windows PowerShell functions, the Windows PowerShell prompt resides on the Function drive. I can use the <b>Get-Content<\/b> cmdlet to retrieve the current value. Here is the one on my computer:<\/p>\n<p style=\"margin-left:30px\">PS C:\\Users\\ed&gt; Get-Content Function:\\prompt<\/p>\n<p style=\"margin-left:30px\">&quot;PS $($executionContext.SessionState.Path.CurrentLocation)$(&#039;&gt;&#039; * ($nestedPromptLevel + 1)) &quot;<\/p>\n<p style=\"margin-left:30px\"># .Link<\/p>\n<p style=\"margin-left:30px\"># http:\/\/go.microsoft.com\/fwlink\/?LinkID=225750<\/p>\n<p style=\"margin-left:30px\"># .ExternalHelp System.Management.Automation.dll-help.xml<\/p>\n<p>OK, it is a little shaggy. The main thing, in fact, the <i>only<\/i> executing code is a one-liner. It retrieves the current location, and it displays a right arrow if I am in a nested prompt. It is basic and generally does what I want. It also lets me have pretty much all the space at the Windows PowerShell console that I could possibly want.<\/p>\n<p>But, it can be shorter. In fact, I know quite a few people who omit the initial <b>PS<\/b>. Here is a prompt that does that:<\/p>\n<p style=\"margin-left:30px\">Function prompt {&quot;$($executionContext.SessionState.Path.CurrentLocation)$(&#039;&gt;&#039; * ($nestedPromptLevel + 1)) &quot;}<\/p>\n<p>All I need to do is to type the command at the Windows PowerShell console, and press ENTER. Here is the command and its results:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I close and re-open the Windows PowerShell console, my Windows PowerShell console prompt returns to the default value.<\/p>\n<p>But suppose that I do not care what my current location is. In that case, I can retrieve a few more spaces on the Windows PowerShell console line. Here is an example of that:<\/p>\n<p style=\"margin-left:30px\">Function prompt {&quot;$(&#039;&gt;&#039; * ($nestedPromptLevel + 1)) &quot;}<\/p>\n<p>The prompt is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In each case, I use the content of the previous function to create my new prompt function. But I do not need to do that. I can, for example, create a prompt that is totally mine. Here is an example that displays the current date:<\/p>\n<p style=\"margin-left:30px\">Function prompt {&quot;HSG $([datetime]::Now.ToShortDateString()): &quot;}<\/p>\n<p>Here is the prompt:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One thing that might be fun to do is change the background color of the Windows PowerShell console&mdash;on a random basis. Here is a prompt that does that:<\/p>\n<p style=\"margin-left:30px\">Function prompt {[console]::backgroundcolor = $(Get-Random -Minimum 0 -Maximum 15); &quot;HSG $([datetime]::Now.ToShortDateString()): &quot;}<\/p>\n<p>Keep in mind that not all colors of a background color will be compatible with the foreground color, so you might want to add a bit of logic if you really want random colors. Each time a Windows PowerShell command runs, the background color changes, but the foreground color is currently remaining the same. Here is what that might look like:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-28-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Remember that if things don&rsquo;t work out, all you need to do is close Windows PowerShell and re-open it. This is because you are creating a temporary function that is only stored in memory. I can redefine it at will and on-the-fly. If I come up with something I really like, I can add it to my Windows PowerShell profile. I will talk about that later.<\/p>\n<p>For now, have fun, and let me know what crazy ideas you come up with.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about playing with the Windows PowerShell prompt. Microsoft Scripting Guy, Ed Wilson, is here. It is time to round up all of those end-of-the-year kinds of things. This is, in fact, the last weekend Hey, Scripting Guy! Blog post of 2014. Not sure what that means; but it [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,563,3,4,61,45],"class_list":["post-119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-prompt","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about playing with the Windows PowerShell prompt. Microsoft Scripting Guy, Ed Wilson, is here. It is time to round up all of those end-of-the-year kinds of things. This is, in fact, the last weekend Hey, Scripting Guy! Blog post of 2014. Not sure what that means; but it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}