{"id":977,"date":"2014-07-19T00:01:00","date_gmt":"2014-07-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/19\/weekend-scripter-exploring-powershell-profiles\/"},"modified":"2014-07-19T00:01:00","modified_gmt":"2014-07-19T00:01:00","slug":"weekend-scripter-exploring-powershell-profiles","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-exploring-powershell-profiles\/","title":{"rendered":"Weekend Scripter: Exploring Powershell Profiles"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, explores Windows PowerShell profiles and locations.\nMicrosoft Scripting Guy, Ed Wilson, is here. One of the cool things about the weekend is that it provides two entire days with no meetings, no specific obligations, and it gives me time to think and reflect. This is also a time to play with Windows PowerShell.\nSince Windows PowerShell&nbsp;1.0 came out, the <b>$profile<\/b> automatic variable points to the CurrerentUserCurrentHost Windows PowerShell profile. This is extremely helpful because otherwise it would be a heck of a lot of typing, even when using tab expansion. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $PROFILE<\/p>\n<p style=\"margin-left:30px\">C:UsersedDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1<\/p>\n<p style=\"margin-left:30px\">PS C:&gt;\nSee what I mean? Dude. That is a lot of typing. But I do not have to type any of that, because <b>$profile<\/b> points to the location. The really cool thing is that it points to that location even if I do not have a profile. That is sweet!\nBut Windows PowerShell has more than one profile. In fact, there are six when one takes into account the Windows PowerShell console and the Windows PowerShell ISE. For more information, see <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/05\/21\/understanding-the-six-powershell-profiles.aspx\" target=\"_blank\">Understanding the Six PowerShell Profiles<\/a>. I have also written about the profiles, using them, and best practices for managing them. To read more about profiles, see these <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/profiles\/\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<h2>Does a profile exist?<\/h2>\n<p>I ran into an issue the other day, in that Windows PowerShell was acting a bit funky. I looked in my Windows PowerShell console profile, but I did not see anything wrong. I thought, &#8220;Hmmmm. I must have added something somewhere else. But what is the easiest way to see if a profile exists?&#8221;\nWell, it must be to list out the profile paths, and use <b>Test-Path<\/b>. If I use <b>Get-Member<\/b> to list the note properties, and use <b>Force<\/b> to show hidden properties, I can find the names of the profiles, in additon to the path.\nThe problem is that to get the path, I would have to use a regular expression to parse the string. This is because <b>$profile<\/b> returns a string, and the extra profiles are added in as note properties. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $PROFILE | gm -MemberType NoteProperty -Force<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; TypeName: System.String<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp; Definition<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">AllUsersAllHosts&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NoteProperty System.String AllUsersAllHosts=C:WindowsSystem32WindowsPowerShellv1.0profil&#8230;<\/p>\n<p style=\"margin-left:30px\">AllUsersCurrentHost&nbsp;&nbsp;&nbsp; NoteProperty System.String AllUsersCurrentHost=C:WindowsSystem32WindowsPowerShellv1.0Mic&#8230;<\/p>\n<p style=\"margin-left:30px\">CurrentUserAllHosts&nbsp;&nbsp;&nbsp; NoteProperty System.String CurrentUserAllHosts=C:UsersedDocumentsWindowsPowerShellprofil&#8230;<\/p>\n<p style=\"margin-left:30px\">CurrentUserCurrentHost NoteProperty System.String CurrentUserCurrentHost=C:UsersedDocumentsWindowsPowerShellMic&#8230;\nI do not like to parse strings with regular expressions if I don&rsquo;t have to do so. And I know that I can find a specific path to a specific profile by adding a dot to the end of <strong>$profile<\/strong>&nbsp;and calling it by name. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $PROFILE.AllUsersAllHosts<\/p>\n<p style=\"margin-left:30px\">C:WindowsSystem32WindowsPowerShellv1.0profile.ps1\nHmmm, so I need to store the profile names in an array, and walk through them after I append them to <b>$profile<\/b>. First I get the profile names and store them in an array, as shown here:<\/p>\n<p style=\"margin-left:30px\">$p = $PROFILE | gm -MemberType NoteProperty -Force | % {$_.name}\nI now check to see if it worked:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $p[0]<\/p>\n<p style=\"margin-left:30px\">AllUsersAllHosts\nYep. So far so good. Now, I decide to walk through the array:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $p | foreach {$PROFILE.$_}<\/p>\n<p style=\"margin-left:30px\">C:WindowsSystem32WindowsPowerShellv1.0profile.ps1<\/p>\n<p style=\"margin-left:30px\">C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1<\/p>\n<p style=\"margin-left:30px\">C:UsersedDocumentsWindowsPowerShellprofile.ps1<\/p>\n<p style=\"margin-left:30px\">C:UsersedDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1\nOK. Now I need to find out which profiles exist. I use the <strong>Test-Path<\/strong> cmdlet to do this. Here is the command:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $p | foreach {$PROFILE.$_ ; Test-Path $profile.$_}<\/p>\n<p style=\"margin-left:30px\">C:WindowsSystem32WindowsPowerShellv1.0profile.ps1<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p style=\"margin-left:30px\">C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p style=\"margin-left:30px\">C:UsersedDocumentsWindowsPowerShellprofile.ps1<\/p>\n<p style=\"margin-left:30px\">False<\/p>\n<p style=\"margin-left:30px\">C:UsersedDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1<\/p>\n<p style=\"margin-left:30px\">True\nI can see that, in fact, I only have one profile for my Windows PowerShell console. There are still two other profiles that I need to check via the Windows PowerShell ISE. But because my issue was in the console, I need to go into the various modules that I use. Anyway, now I have a starting point. See you tomorrow.\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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, explores Windows PowerShell profiles and locations. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about the weekend is that it provides two entire days with no meetings, no specific obligations, and it gives me time to think and reflect. This is also a time to [&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,61,45],"class_list":["post-977","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, explores Windows PowerShell profiles and locations. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about the weekend is that it provides two entire days with no meetings, no specific obligations, and it gives me time to think and reflect. This is also a time to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/977","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=977"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/977\/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=977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}