{"id":253,"date":"2017-09-26T20:01:29","date_gmt":"2017-09-27T02:01:29","guid":{"rendered":"http:\/\/draith.com\/?p=253"},"modified":"2017-09-26T20:01:29","modified_gmt":"2017-09-27T02:01:29","slug":"customize-your-powershell-profile-for-useful-startup-actions","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=253","title":{"rendered":"Customize your PowerShell profile for useful startup actions"},"content":{"rendered":"<p>Did you know you can make PowerShell run any commands you want when you start a shell?  This is amazingly useful for gathering information, making settings changes, or kicking off processes &#8211; all at shell startup.  There are plenty of places that talk about the profiles, so I won&#8217;t go into each type, but long story short there are almost 10 different profiles when you account for 32 and 64 bit PowerShell.  Some of them are explained in detail <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/bb613488(v=vs.85).aspx\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a><\/p>\n<p>For my example here, I am going to deal with the <em>%UserProfile%\\My Documents\\WindowsPowerShell\\profile.ps1 <\/em> profile, which affects the current user, but all shells.  This is useful for when you are switching back and forth between the ISE and console &#8211; say when you are testing new scripts. By default this file won&#8217;t exist &#8211; you will have to create it if it doesn&#8217;t.  <\/p>\n<pre class=\"lang:ps decode:true \" title=\"Find your profile\" >#See if your profile file exists.  Checks the 'My Documents\\WindowsPowerShell' directory\nTest-Path $profile<\/pre>\n<p>The profile file is really just a .ps1 file.  You can put any PowerShell you want in this file.  Say you want to get a random <a href=\"https:\/\/catfact.ninja\" target=\"_blank\" rel=\"noopener noreferrer\">Cat Fact<\/a> every time you start a shell? (who am I to judge?)<\/p>\n<pre class=\"lang:ps decode:true \" >[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n((((invoke-webrequest -uri https:\/\/catfact.ninja\/fact -Method get).content).split(':')[1])).trim('\",\"length\"')\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/09\/profile1.png\" alt=\"\" width=\"1349\" height=\"215\" class=\"alignnone size-full wp-image-256\" srcset=\"\/wp-content\/uploads\/2017\/09\/profile1.png 1349w, \/wp-content\/uploads\/2017\/09\/profile1-300x48.png 300w, \/wp-content\/uploads\/2017\/09\/profile1-1024x163.png 1024w, \/wp-content\/uploads\/2017\/09\/profile1-768x122.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>There are some really useful things you can do now that you know you can run anything.  For example &#8211; this will show the number of running PowerShell processes, along with WinRM service status:<\/p>\n<pre class=\"lang:ps decode:true \" >write-host -ForegroundColor Green \"PowerShell Processes: \" (Get-process 'PowerShell').count\nwrite-host -ForegroundColor Green \"WinRM Status: \" (Get-service 'winrm').status<\/pre>\n<p>And this will show the PowerShell module paths:<\/p>\n<pre class=\"lang:ps decode:true \" >write-host -ForegroundColor Green \"Module Paths:\"\nforeach ($module in ($env:PSModulePath).Split(\";\")){write-host -ForegroundColor Green \"    \"$module}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/09\/profile2.png\" alt=\"\" width=\"731\" height=\"259\" class=\"alignnone size-full wp-image-259\" srcset=\"\/wp-content\/uploads\/2017\/09\/profile2.png 731w, \/wp-content\/uploads\/2017\/09\/profile2-300x106.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Notice here &#8211; I also added a simple &#8220;cd c:\\blog&#8221; to my profile &#8211; really useful for starting straight in your scripts directory (no more &#8220;cd c:\\workspaces\\app\\development\\scripts&#8230;.&#8221; every time you start a console)<\/p>\n<p>But you can do even more!  Full functions can be loaded into your profile and are available immediately.  One of my favorites is one that will add a Start-RDP function, so I can initiate a remote desktop session without ever touching the start menu!  How cool is this?<\/p>\n<pre class=\"lang:ps decode:true \" >write-host -ForegroundColor Green \"PowerShell Processes: \" (Get-process 'PowerShell').count\nwrite-host -ForegroundColor Green \"WinRM Status: \" (Get-service 'winrm').status\nwrite-host -ForegroundColor Green \"Module Paths:\"\nforeach ($module in ($env:PSModulePath).Split(\";\")){write-host -ForegroundColor Green \"    \"$module}\ncd C:\\Blog\nfunction Start-RDP\n{\n    param  \n    (  \n        [Parameter(\n            Position = 0,\n            ValueFromPipeline=$true,\n            Mandatory=$true\n        )]\n        [ValidateNotNullOrEmpty()]\n        [string]\n        $ServerName\n    )\n    mstsc \/v:$ServerName \n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/09\/profile3.png\" alt=\"\" width=\"1341\" height=\"933\" class=\"alignnone size-full wp-image-261\" srcset=\"\/wp-content\/uploads\/2017\/09\/profile3.png 1341w, \/wp-content\/uploads\/2017\/09\/profile3-300x209.png 300w, \/wp-content\/uploads\/2017\/09\/profile3-1024x712.png 1024w, \/wp-content\/uploads\/2017\/09\/profile3-768x534.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>There is also a special function you can put in your profile &#8211; it&#8217;s the &#8216;prompt&#8217; function.  This will change the PowerShell command line prompt to whatever you want!  Just create a new function called &#8216;prompt&#8217;, write-host anything you want into the function, and make sure you put a &#8216;return &#8221; &#8220;&#8216; at the end &#8211; it&#8217;s that&#8217;s simple!  You can put some great data right on the prompt &#8211; for example, you can make the current time show up each time the prompt is shown!  This is really useful for measuring how long something takes to run if you don&#8217;t want to pull out measure-object!  Here is my prompt, along with the full profile:<\/p>\n<pre class=\"lang:ps decode:true \" >function prompt\n{\n    $time = \"(\"+(get-date).ToLongTimeString()+\")\"\n    write-host -NoNewline -ForegroundColor Green \"PS \" (Get-Location).Path $time \"&gt;\"\n    return \" \"\n}\nwrite-host -ForegroundColor Green \"PowerShell Processes: \" (Get-process 'PowerShell').count\nwrite-host -ForegroundColor Green \"WinRM Status: \" (Get-service 'winrm').status\nwrite-host -ForegroundColor Green \"Module Paths:\"\nforeach ($module in ($env:PSModulePath).Split(\";\")){write-host -ForegroundColor Green \"    \"$module}\ncd C:\\Blog\nfunction Start-RDP\n{\n    param  \n    (  \n        [Parameter(\n            Position = 0,\n            ValueFromPipeline=$true,\n            Mandatory=$true\n        )]\n        [ValidateNotNullOrEmpty()]\n        [string]\n        $ServerName\n    )\n    mstsc \/v:$ServerName \n}<\/pre>\n<p>And the outcome:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/09\/profile4.png\" alt=\"\" width=\"745\" height=\"322\" class=\"alignnone size-full wp-image-262\" srcset=\"\/wp-content\/uploads\/2017\/09\/profile4.png 745w, \/wp-content\/uploads\/2017\/09\/profile4-300x130.png 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>It&#8217;s that simple!  Customize your profile, and start being productive quicker!  Leave a comment below to tell me what your favorite profile modifications are!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know you can make PowerShell run any commands you want when you start a shell? This is amazingly useful for gathering information, making settings changes, or kicking off processes &#8211; all at shell startup. There are plenty of places that talk about the profiles, so I won&#8217;t go into each type, but long &hellip; <a href=\"https:\/\/draith.com\/?p=253\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Customize your PowerShell profile for useful startup actions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":0,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}