{"id":72002,"date":"2015-07-22T00:01:00","date_gmt":"2015-07-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/22\/powershell-profile-which-one\/"},"modified":"2019-02-18T09:46:54","modified_gmt":"2019-02-18T16:46:54","slug":"powershell-profile-which-one","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-profile-which-one\/","title":{"rendered":"PowerShell Profile? Which One?"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about determining which Windows PowerShell profile to use for which functions.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Living in central Florida can be a bit warm and humid in late July. Of course, I knew that before we moved here. I told one of my friends recently, when he commented on the heat and humidity, that it was truth in advertising. I told him if the Scripting Wife survived July and August here, she would know that she could handle the worst of the summer weather.\nInterestingly enough, on many days recently, it has actually been warmer back in Charlotte, North Carolina. Of course, the humidity and the UV Index have been higher here. But when one is talking about 95 degrees Fahrenheit, it really does not make that much difference. It&rsquo;s not like we are going to be outside running a marathon at noon.\nBut it is a great time to sit under the big ZZ Top tree in the back yard, and work on my new Windows PowerShell profile. I have also been making Gun Powder green tea, with hibiscus flowers, rose hips, and orange blossom honey, and then sticking it in the refrigerator. It is wonderfully refreshing, and it has just enough of a citrus flavor to it. It is my version of &ldquo;southern iced tea.&rdquo;<\/p>\n<h2>Use the right function in the right profile<\/h2>\n<p>There are differences between the Windows PowerShell ISE and the Windows PowerShell console host. This is obvious. There are also some things that I simply cannot do in the Windows PowerShell ISE. So when I am writing my Windows PowerShell profile, it makes sense to add some checks that make sure I am not trying to do something that I am not permitted to do.\nFor example, I cannot start the Windows PowerShell transcript tool in the Windows PowerShell ISE because it does not support the transcript (at least not as of Windows PowerShell&nbsp;4.0). Also, the Windows PowerShell ISE output pane does not support the pager&hellip;that is <b>More<\/b>.\nSo when I output a multiple page output in the Windows PowerShell console, I can pipe the output to <b>More<\/b>, and I will see one page at a time. In the Windows PowerShell ISE, I don&rsquo;t get an error, but I do not get paged output either. So, it makes sense to not even call these sorts of commands in the Windows PowerShell ISE.\nWhat I need is a good way to determine if I am running in the Windows PowerShell console, or if I am running in the Windows PowerShell ISE.<\/p>\n<h2>Test-ConsoleHost<\/h2>\n<p>I can create a simple function, called <b>Test-ConsoleHost<\/b>, that will determine if I am running in the Windows PowerShell console. To do this, I look at the <b>Name<\/b> property of the <b>$host<\/b> automatic variable. If it matches <b>ConsoleHost<\/b>, then I return <b>True<\/b>, otherwise I return <b>False<\/b>. Here is the function:<\/p>\n<p style=\"margin-left:30px\">Function Test-ConsoleHost<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;if(($host.Name -match &#8216;consolehost&#8217;)) {$true}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Else {$false}&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">}\nWhen I run load the function into the Windows PowerShell ISE, I can call the function directly. Because it returns <b>$false<\/b> if it is not in the Windows PowerShell console, it will return <b>$false<\/b> in the Windows PowerShell ISE. Here I check for the not true condition, and therefore, it returns the ISE name:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; if(-not (Test-ConsoleHost)) {&#8220;ise&#8221;}<\/p>\n<p style=\"margin-left:30px\">ise\nI add the <b>Test-ConsoleHost<\/b> function to my Windows PowerShell profile. I also create an alias for it called <b>tch<\/b>. This is shown here in my Windows PowerShell profile:<\/p>\n<p style=\"margin-left:30px\">#Aliases<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name ep -Value edit-profile | out-null<\/p>\n<p style=\"margin-left:30px\">Set-Alias -Name tch -Value Test-ConsoleHost | out-null<\/p>\n<p style=\"margin-left:30px\">#Functions<\/p>\n<p style=\"margin-left:30px\">Function Edit-Profile<\/p>\n<p style=\"margin-left:30px\">{ ISE $profile }<\/p>\n<p style=\"margin-left:30px\">Function Test-ConsoleHost<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;if(($host.Name -match &#8216;consolehost&#8217;)) {$true}<\/p>\n<p style=\"margin-left:30px\">&nbsp;Else {$false}&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">#Variables<\/p>\n<p style=\"margin-left:30px\">New-Variable -Name doc -Value &#8220;$home\\documents&#8221;<\/p>\n<p style=\"margin-left:30px\">#PS_Drives<\/p>\n<p style=\"margin-left:30px\">New-PSDrive -Name Mod -Root ($env:PSModulePath -split &#8216;;&#8217;)[0] `<\/p>\n<p style=\"margin-left:30px\">&nbsp;-PSProvider FileSystem | out-null<\/p>\n<p style=\"margin-left:30px\">#Commands<\/p>\n<p style=\"margin-left:30px\">Set-Location c:\\<\/p>\n<p style=\"margin-left:30px\">Start-Transcript\nNow when I close the Windows PowerShell console and open it again, I can call the <b>Test-ConsoleHost<\/b> function directly via the alias. As shown here, it works:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; tch<\/p>\n<p style=\"margin-left:30px\">True\nNow I can wrap a bit of code around it, and use the <b>Test-ConsoleHost<\/b> function to get my Windows PowerShell host. As shown here, it also works:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; if(tch) {&#8220;this is the powershell console&#8221;}<\/p>\n<p style=\"margin-left:30px\">this is the powershell console<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt;\nNow, if I want to, I can &ldquo;safeguard&rdquo; my <b>Start-Transcript<\/b> command by doing a Windows PowerShell host check:<\/p>\n<p style=\"margin-left:30px\">If(tch) {Start-Transcript}\nThat is all there is to adding functions to your Windows PowerShell console profile. Profile Week will continue tomorrow when I will talk about more cool stuff.\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about determining which Windows PowerShell profile to use for which functions. Microsoft Scripting Guy, Ed Wilson, is here. Living in central Florida can be a bit warm and humid in late July. Of course, I knew that before we moved here. I told one of my friends recently, [&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":[144,3,4,45],"class_list":["post-72002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about determining which Windows PowerShell profile to use for which functions. Microsoft Scripting Guy, Ed Wilson, is here. Living in central Florida can be a bit warm and humid in late July. Of course, I knew that before we moved here. I told one of my friends recently, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72002","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=72002"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72002\/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=72002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}