{"id":51953,"date":"2009-11-23T00:01:00","date_gmt":"2009-11-23T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/11\/23\/hey-scripting-guy-how-can-i-use-profiles-with-windows-powershell\/"},"modified":"2009-11-23T00:01:00","modified_gmt":"2009-11-23T00:01:00","slug":"hey-scripting-guy-how-can-i-use-profiles-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-profiles-with-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Profiles with Windows PowerShell?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>     &nbsp;<\/p>\n<p><br>!&#8211;&gt;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I would like to personalize the way that Windows PowerShell works. I have been hearing that I can use a thing called a profile to do this, but when I try to find information about profiles, I come up blank. There is no New-Profile Windows PowerShell cmdlet so I do not see how to create such a thing. Can you help me please?<\/p>\n<p class=\"MsoNormal\">&#8212; AC<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello AC, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. It is another beautiful day in Charlotte, North Carolina&mdash;at least it would be. I had an early morning visit to the doctor this morning, and you know how that can go. Interestingly enough, one of the nurses commented to the Scripting Wife that I was not a morning person because I was always grumpy when she saw me. Not a morning person? I get up early every day of the week. Grumpy? Me? I am one of the most laid back people you could ever meet. Then it dawned on me. The only time she sees me, she is giving me a shot or draining a couple quarts of blood from my veins. That must be why she thinks I am grumpy. <\/p>\n<p class=\"MsoNormal\">AC, it is interesting you talk about changing the way Windows PowerShell behaves because Windows PowerShell is highly configurable. The way you see it today may not be the way you see it tomorrow. If you were to use Windows PowerShell on my computer, it might behave differently. The environment controls it all&mdash;and the Windows PowerShell profile is a major part of that environment. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows PowerShell 2.0 Best Practices book\" alt=\"Image of Windows PowerShell 2.0 Best Practices book\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1123\/hsg-11-23-09-03.jpg\" width=\"197\" height=\"240\"><\/p>\n<p class=\"Readeraidonly\">Note: Portions of today&#8217;s Hey, Scripting Guy! article are excerpted from the Microsoft Press book, <em>Windows PowerShell 2.0 Best Practices<\/em> by Ed Wilson. This book is <a href=\"http:\/\/bit.ly\/1NBxZ0\"><font face=\"Segoe\">available for pre-order<\/font><\/a>.<\/p>\n<h2><br>Creating the profile<\/h2>\n<p class=\"MsoNormal\">When Windows PowerShell is first installed, there are no profiles installed on the computer. In one respect you can consider the profile to be like the autoexec.bat file was a few years ago. The autoexec.bat file was simply a batch file that executed batch commands. However, because it was located in the root and had the name autoexec.bat, it took on an importance that was greatly out of proportion to a simple batch file because the commands that existed in the file were used to configure all kinds of activities including configuring the environment and even launching Windows itself. The Windows PowerShell profile does not launch Windows PowerShell. Instead, it is a Windows PowerShell script that happens to have a special name and happens to exist in a special place, or rather it happens to have two special names and exists in four special places. You heard me correctly. There are actually <i>four<\/i> Windows PowerShell profiles. These four profiles are listed in Table 1.<\/p>\n<p class=\"TableNum-Title\"><strong>Table 1<span>&emsp;<\/span>Windows PowerShell Profiles and Locations<\/strong><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"581\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"130\">\n<p class=\"TableHead\"><strong>Profile<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"451\">\n<p class=\"TableHead\"><strong>Location<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"130\">\n<p class=\"TableText\">AllUsersAllHosts<\/p>\n<\/td>\n<td valign=\"top\" width=\"451\">\n<p class=\"TableText\">C:Windowssystem32WindowsPowerShellv1.0profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"130\">\n<p class=\"TableText\">AllUsersCurrentHost<\/p>\n<\/td>\n<td valign=\"top\" width=\"451\">\n<p class=\"TableText\">C:Windowssystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"130\">\n<p class=\"TableText\">CurrentUserAllHosts<\/p>\n<\/td>\n<td valign=\"top\" width=\"451\">\n<p class=\"TableText\">C:Users<i>UserName<\/i>DocumentsWindowsPowerShellprofile.ps1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"130\">\n<p class=\"TableText\">CurrentUserCurrentHost<\/p>\n<\/td>\n<td valign=\"top\" width=\"451\">\n<p class=\"TableText\">C:Users<i>UserName<\/i>DocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">In addition to the four Windows PowerShell profiles, other applications that host Windows PowerShell can create their own profiles. For example, the Windows PowerShell Integrated Scripting Environment (ISE) hosts Windows PowerShell and has two additional profiles. These two profiles are listed in Table 2.<\/p>\n<p class=\"TableNum-Title\"><strong>Table 2&emsp;Windows PowerShell ISE Profiles and Locations<\/strong><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"139\">\n<p class=\"TableHead\"><strong>Profile<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"427\">\n<p class=\"TableHead\"><strong>Location<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">\n<div>\n<p class=\"TableText\">CurrentUserCurrentHost<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"427\">\n<div>\n<p class=\"TableText\"><span>&nbsp;<\/span>$Home[My ]DocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">\n<div>\n<p class=\"TableText\">AllUsersCurrentHost<\/p>\n<\/div>\n<\/td>\n<td valign=\"top\" width=\"427\">\n<div>\n<p class=\"TableText\"><span>&nbsp;<\/span>$PsHomeMicrosoft.PowerShellISE_profile.ps1<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><font size=\"3\">Choosing the correct profile<\/font><\/h3>\n<p class=\"MsoNormal\">Two of the profiles are used by all Windows PowerShell users on a computer. Anything placed in the All Users profile will be available to any script or to any user that runs Windows PowerShell on the computer. As a result, you should be rather circumspect in what you place in the All Users profile. However, these are great locations to configure aliases that you wish to make available to all users, variables you intend to use in a corporate scripting environment, or a Windows PowerShell drive or a function. <\/p>\n<p class=\"MsoNormal\">The next question is which one of the two All Users profiles should you use? The AllUsersAllHosts profile will apply to all of the users on the computer. It also applies to every instance of Windows PowerShell that may run on the computer. This includes the Windows PowerShell console, the Windows PowerShell ISE, and anything else that may host Windows PowerShell. This would include the Exchange Management environment, the SQL console, and any application that could host Windows PowerShell. Even if you are careful with the aliases you create, the variables you assign, the functions you write, and any Windows PowerShell drives you decide to make, you will still need to test to ensure compatibility. The AllUsersCurrentHost profile would give you the same ability to modify the Windows PowerShell environment for all users, but would only apply to the console host. <\/p>\n<p class=\"MsoNormal\">The two current user profiles are used to modify the Windows PowerShell environment for the current user. The profile that is most often modified by a user to configure personal Windows PowerShell settings is the CurrentUserCurrentHost profile. This one is referenced by the <b>$profile<\/b> automatic variable. On my computer, the value of the <b>$profile<\/b> variable is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; $PROFILE<br>C:UsersedwilsonDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You can see that, on a Windows Vista computer (or on a Windows 7 computer), the user&#8217;s personal folder is in the user&rsquo;s Documents folder. The WindowsPowerShell folder does not exist if no profile has been created. This is seen here, where the <b>Test-Path<\/b> cmdlet is used to see if the parent folder that should contain the Microsoft.Powershell_profile.ps1 file exists. Because no personal profile has yet been created on this laptop, the WindowsPowerShell folder has not been created: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Test-path (Split-Path $PROFILE -Parent)<br>False <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Keep in mind that some applications will create an application-specific profile and store it in the WindowsPowerShell folder. On one of my computers, I do not have a Windows PowerShell profile configured. I do, however, have Sapien&rsquo;s Primal Script installed, and Primal Script creates an application-specific profile. The test for the parent folder, therefore, will pass as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Test-Path (Split-Path $PROFILE -Parent)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">True<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Test-Path $PROFILE<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">False<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">To explore what is contained in the <b>$PROFILE &ndash;Parent<\/b> folder, you can call Windows Explorer from the command line by using the <b>Invoke-Item<\/b> cmdlet. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Invoke-Item -Path (Split-Path $PROFILE -Parent)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">You can also call Windows Explorer directly by using the executable name as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; explorer (Split-Path $PROFILE -parent)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Calling Windows Explorer directly does not really buy you anything, particularly because you can use aliases to shorten the typing. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; ii (Split-Path $PROFILE -Par)<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">However you get there, the Windows Explorer directory is set to the WindowsPowerShell directory. This is seen here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows Explorer directory set to the Windows PowerShell directory\" alt=\"Image of Windows Explorer directory set to the Windows PowerShell directory\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1123\/hsg-11-23-09-01.jpg\" width=\"600\" height=\"421\"><\/p>\n<p class=\"MsoNormal\"><br>To create a CurrentUserCurrentHost profile, use the <b>New-Item<\/b> cmdlet as shown in the following code. When using the <b>New-Item<\/b> cmdlet, specify the <b>&ndash;force<\/b> if the folder does not exist, and specify the <b>ItemType<\/b> as <b>file<\/b>. This is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">New-Item -Path $PROFILE -ItemType file &ndash;Force<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">After you create the profile, you can open it in Notepad or in the Windows PowerShell ISE to edit the file. If you choose to edit it in Notepad, it is as simple as typing <b>Notepad<\/b> and giving it the automatic <b>$profile<\/b> variable as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Notepad $profile<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The newly created Windows PowerShell profile opens in Notepad and is ready for editing. This is seen here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows PowerShell profile ready for editing\" alt=\"Image of Windows PowerShell profile ready for editing\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1123\/hsg-11-23-09-02.jpg\" width=\"481\" height=\"256\"><\/p>\n<p class=\"MsoNormal\"><br>Of course, AC, there is nothing in the Windows PowerShell profile at this stage of the game. We will add to our profile tomorrow as Windows PowerShell Profile Week continues. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/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, keep on scripting!<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; !&#8211;&gt; Hey, Scripting Guy! I would like to personalize the way that Windows PowerShell works. I have been hearing that I can use a thing called a profile to do this, but when I try to find information about profiles, I come up blank. There is no New-Profile Windows PowerShell cmdlet so I do [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,144,3,4,45],"class_list":["post-51953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; !&#8211;&gt; Hey, Scripting Guy! I would like to personalize the way that Windows PowerShell works. I have been hearing that I can use a thing called a profile to do this, but when I try to find information about profiles, I come up blank. There is no New-Profile Windows PowerShell cmdlet so I do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51953","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=51953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51953\/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=51953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}