{"id":4370,"date":"2013-01-04T00:01:00","date_gmt":"2013-01-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/01\/04\/understanding-and-using-powershell-profiles\/"},"modified":"2013-01-04T00:01:00","modified_gmt":"2013-01-04T00:01:00","slug":"understanding-and-using-powershell-profiles","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/understanding-and-using-powershell-profiles\/","title":{"rendered":"Understanding and Using PowerShell Profiles"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about understanding and using Windows PowerShell profiles.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today, I thought I would provide another excerpt from my new Microsoft Press book: <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-Step\/dp\/0735663394\/ref=sr_1_2?ie=UTF8&amp;qid=1357148960&amp;sr=8-2&amp;keywords=powershell+step+by+step\" target=\"_blank\">Windows PowerShell 3.0 Step by Step, which is available for pre-order now<\/a>. Today&rsquo;s excerpt is from the chapter on Windows PowerShell profiles.<\/p>\n<h2>Six different PowerShell profiles<\/h2>\n<p>Windows PowerShell profiles are a bit confusing. There are, in fact, six different ones. Both the Windows PowerShell console and the Windows PowerShell ISE have their own profiles. In addition, there are profiles for the current user, as well as profiles for all users. Table 9-1 lists the six different profiles and their associated locations.<\/p>\n<p class=\"Num-Caption\">Table 9-1 The six different Windows PowerShell profile paths and use<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p><strong>Description<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p><strong>Path<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">Current User, Current Host &#8211; console<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$Home\\[My ]Documents\\WindowsPowerShell\\Profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">Current User, All Hosts&nbsp;&nbsp;&nbsp;<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$Home\\[My ]Documents\\Profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">All Users, Current Host &#8211; console&nbsp;&nbsp;&nbsp;<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$PsHome\\Microsoft.PowerShell_profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">All Users, All Hosts&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$PsHome\\Profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">Current user, Current Host &#8211; ISE<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$Home\\[My ]Documents\\WindowsPowerShell\\Microsoft.P owerShellISE_profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"161\">\n<p class=\"Normalunindented\">&nbsp;All users, Current Host &#8211; ISE&nbsp;&nbsp;<\/p>\n<\/td>\n<td valign=\"top\" width=\"425\">\n<p class=\"Normalunindented\">$PsHome\\Microsoft.PowerShellISE_profile.ps1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Understanding the six different Windows PowerShell profiles<\/h2>\n<p>The first thing to do in understanding the six different Windows PowerShell profiles is to keep in mind that they move, they change, as long as you realize they are a moving target, you will be fine. In most cases, when talking about the Windows PowerShell profile, people are referring to the <em>current user, current<\/em> <em>host<\/em> profile. In fact, if no one qualifies the Windows PowerShell profile with its associated scope or description, it is safe to assume that they are talking about the <em>current user, current host <\/em>profile.<\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note <\/strong>&nbsp;&nbsp;The Windows PowerShell profile (any one of the six) is simply a Windows PowerShell script. It has a special name, and it resides in a special place, but it is simply a script. In this regard, it is sort of like the old-fashioned <em>autoexec.bat <\/em>batch file. Because the Windows PowerShell profile is a Windows PowerShell script, you must enable the Script Execution policy prior to configuring and using a Windows PowerShell profile.<\/p>\n<h2>Examining the $profile variable<\/h2>\n<p>When you query the $profile automatic variable, it returns the path to the <em>current user, current host<\/em> profile. This makes sense, and it&rsquo;s a great way to easily access the path to the profile. The following illustrates this technique from within the Windows PowerShell console.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $profile<br \/> C:\\Users\\ed.IAMMRED\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1<\/p>\n<p>Inside the Windows PowerShell ISE, when I type query the $profile automatic variable, I receive the output as shown here.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\Users\\ed.IAMMRED&gt; $profile<br \/> C:\\Users\\ed.IAMMRED\\Documents\\WindowsPowerShell\\Microsoft.PowerShell<strong>ISE<\/strong>_profile.ps1<\/p>\n<p>To save you a bit of analyzing &hellip; the difference between the Windows PowerShell console <em>current user, current host<\/em> profile path and the Windows PowerShell ISE <em>current user, current host<\/em> profile path is three letters: <em>ISE. <\/em><\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;These three letters often cause you problems. You are setting something in your Windows PowerShell console profile, and it is not available inside the Windows PowerShell ISE.<\/p>\n<h3>Unraveling the different profiles<\/h3>\n<p>You can pipe the $profile variable to the <strong>Get-Member<\/strong> cmdlet and see additional properties that exist on the $profile variable, as shown here.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $PROFILE | Get-Member -MemberType noteproperty | select name<\/p>\n<p> Name<br \/> &#8212;-<br \/> AllUsersAllHosts<br \/> AllUsersCurrentHost<br \/> CurrentUserAllHosts<br \/> CurrentUserCurrentHost<\/p>\n<p>If accessing the $profile variable from within the Windows PowerShell console, the AllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell console. If you access the $profile variable from within the Windows PowerShell ISE, the AllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell ISE profiles.<\/p>\n<h3>Using the $profile variable to refer to more than current host<\/h3>\n<p>When you reference the $profile variable, by default it refers to the current user, current host profile. If you pipe the variable to the <strong>Format-List<\/strong> cmdlet, it still refers to the current user, current host profile. This technique is shown here.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; $PROFILE | Format-List *<br \/> C:\\Users\\ed.IAMMRED\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1<\/p>\n<p>This leads to a bit of confusion, especially because the <strong>Get-Member<\/strong> cmdlet reveals the existence of multiple profiles and multiple note properties. The way to see all of the profiles for the current host is to use the <strong>force<\/strong><em> <\/em>parameter&mdash;it reveals the hidden properties. The command illustrating this technique is shown here.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">$PROFILE | Format-List * -Force<\/p>\n<p>The command to display the various profiles and the associated output from the command appear in Figure 9-1.<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3404.F09xx01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3404.F09xx01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p class=\"Num-Caption\">Figure 9-1 The $profile variable contains path to several different Windows PowerShell profiles.<\/p>\n<p>It is possible to directly access each of these specific properties&mdash;just like you would access any other property&mdash;via dotted notation, as shown here.<\/p>\n<p class=\"CodeBlock\">$PROFILE.CurrentUserAllHosts<\/p>\n<p>The path to each of the four different profiles for the Windows PowerShell console appears in Figure 9-2.<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4456.F09xx02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4456.F09xx02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p class=\"Num-Caption\">Figure 9-2 Use dotted notation to access the various properties of the $profile variable.<\/p>\n<h2>Determine if a specific profile exists<\/h2>\n<p>To determine if a specific profile exists, use the <strong>Test-Path<\/strong> cmdlet and the appropriate <em>flavor <\/em>of the $profile variable. For example, to determine if a <em>current user, current host <\/em>profile exists, you can use the $profile variable with no modifier, or you can also use the currentusercurrenthost note property. The following example illustrates both of these.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; test-path $PROFILE<br \/> True<br \/> PS C:\\&gt; test-path $PROFILE.CurrentUserCurrentHost<br \/> True<br \/> PS C:\\&gt;<\/p>\n<p>In the same manner, the other three profiles that apply to the current host (in this example, I am using the Windows PowerShell console) are determined not to exist. This is shown in the code that follows.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; test-path $PROFILE.AllUsersAllHosts<br \/> False<br \/> PS C:\\&gt; test-path $PROFILE.AllUsersCurrentHost<br \/> False<br \/> PS C:\\&gt; test-path $PROFILE.CurrentUserAllHosts<br \/> False<br \/> PS C:\\&gt;<\/p>\n<p>Join me tomorrow when I will talk about more cool Windows PowerShell stuff.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about understanding and using Windows PowerShell profiles. Microsoft Scripting Guy, Ed Wilson, is here. Today, I thought I would provide another excerpt from my new Microsoft Press book: Windows PowerShell 3.0 Step by Step, which is available for pre-order now. Today&rsquo;s excerpt is from the chapter on Windows [&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-4370","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: Microsoft Scripting Guy, Ed Wilson, talks about understanding and using Windows PowerShell profiles. Microsoft Scripting Guy, Ed Wilson, is here. Today, I thought I would provide another excerpt from my new Microsoft Press book: Windows PowerShell 3.0 Step by Step, which is available for pre-order now. Today&rsquo;s excerpt is from the chapter on Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4370","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=4370"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4370\/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=4370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}