{"id":1302,"date":"2014-05-24T00:01:00","date_gmt":"2014-05-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/24\/whats-in-your-powershell-profile-powershell-team-favorites\/"},"modified":"2014-05-24T00:01:00","modified_gmt":"2014-05-24T00:01:00","slug":"whats-in-your-powershell-profile-powershell-team-favorites","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/whats-in-your-powershell-profile-powershell-team-favorites\/","title":{"rendered":"What&#8217;s in Your PowerShell Profile? Powershell Team Favorites"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy Ed Wilson talks to members of the Windows PowerShell team about what is in their Windows PowerShell profile.\nMicrosoft Scripting Guy, Ed Wilson, is here. I&#8217;m wrapping up the week with profile excerpts from the Windows PowerShell team. There is some really cool stuff here.\n<span style=\"font-size:large\"><b>Hemant Mahawar<\/b><\/span> indicated that he does not use a profile.\nThis is quite common for people who work in Windows PowerShell core. This is also a common theme I have run across from people who teach or otherwise work with customers on a regular basis. The danger, of course, is inadvertently picking up something that is in your profile, and expecting it to be in a default Windows PowerShell installation.\n<span style=\"font-size:large\"><strong>Lee Holmes<\/strong><\/span> shared a couple of really cool techniques.\n&#8220;Here are two killer techniques I use in my profile.&#8221;<\/p>\n<h4><b>Use PSDefaultParameterValues on Out-Default<\/b><\/h4>\n<p>&#8220;This is only supported on Windows PowerShell 4.0 and later in Windows 8.1), but it lets you capture the output of the last command into a variable (I use <b>$0<\/b>).&#8221;<\/p>\n<p style=\"margin-left:30px\">123 [C:windowssystem32]<\/p>\n<p style=\"margin-left:30px\">&gt;&gt; $PSDefaultParameterValues[&#8220;Out-Default:OutVariable&#8221;] = &#8220;0&#8221;\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">124 [C:windowssystem32]<\/p>\n<p style=\"margin-left:30px\">&gt;&gt; Get-Command Watch-Clipboard\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">CommandType&nbsp;&nbsp;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sp;<\/p>\n<p style=\"margin-left:30px\">125 [C:windowssystem32]<\/p>\n<p style=\"margin-left:30px\">&gt;&gt; ise $0.Source<\/p>\n<h4><b>Auto-register scheduled jobs<\/b><\/h4>\n<p>&#8220;My Windows PowerShell jobs are constantly being blasted from my machines. So I put their registration in my profile. When Windows PowerShell launches, it lets me know if the job is missing. If it is, it re-registers it.&#8221;<\/p>\n<p style=\"margin-left:30px\">if(-not (Get-ScheduledJob -Name PowerFix -EA Ignore))<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration ([TimeSpan]::MaxValue)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $null = Register-ScheduledJob -Name PowerFix -Trigger $trigger -ScriptBlock { powercfg -x -standby-timeout-ac 0 } -MaxResultCount 1<\/p>\n<p style=\"margin-left:30px\">}\n<span style=\"font-size:large\"><strong>Steve Lee<\/strong><\/span> is a principal test lead in Windows PowerShell and WMI, and he provided links to several resources.<\/p>\n<ul>\n<li>He put together his experiences in Windows PowerShell and CIM\/WS-Man to publish&nbsp;<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/PowerShell-Out-of-Band-84009c12\" target=\"_blank\">PowerShell Out-of-Band Hardware Management Module<\/a>.<\/li>\n<li>Steve has written several technical posts on the <a href=\"http:\/\/blogs.msdn.com\/b\/wmi\/\" target=\"_blank\">Windows Management Infrastructure Blog<\/a> that demonstrate how to use Windows PowerShell script to perform standards-based management tasks.&nbsp;<\/li>\n<li>He is an active contributing member of the <a href=\"http:\/\/dmtf.org\/\" target=\"_blank\">DMTF<\/a> and editor of several <a href=\"http:\/\/dmtf.org\/standards\/profiles\" target=\"_blank\">Management Profiles<\/a>.&nbsp;<\/li>\n<\/ul>\n<p><span style=\"font-size:large\"><strong>Abhik Chatterjee<\/strong><\/span> provided a useful function to use when you are dealing with nested directories:\n&#8220;I don&rsquo;t have much in my Windows PowerShell profile, but I find this function really useful because when I am in a very nested directory, it puts the prompt right below it so I do not have to scroll a lot.&#8221;<b><\/b><\/p>\n<p style=\"margin-left:30px\">function prompt<b><\/b><\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host(&#8220;PS: &#8221; + &#8220;$pwd&#8221; + &#8220;&gt;&#8221;)<b><\/b><\/p>\n<p style=\"margin-left:30px\">}\n<span style=\"font-size:large\"><strong>Jason Shirk<\/strong><\/span> offered the following suggestion for changing directories.\n&#8220;Here is a &#8216;command not found&#8217; handler that I use to change directories without typing <b>cd<\/b>.&#8221;<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;$ExecutionContext.InvokeCommand.CommandNotFoundAction =<\/p>\n<p style=\"margin-left:30px\">\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; param([string]$commandName,<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.Management.Automation.CommandLookupEventArgs]$eventArgs)<b><\/b>\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; # Remove the &#8216;get-&#8216; prefix that confuses Test-Path after we produce<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; # something that is path like after the get-.<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; if ($commandName.StartsWith(&#8216;get-&#8216;))<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; {<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $commandName = $commandName.Substring(4)<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; }<b><\/b>\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; # Replace sequences of 3 or more dots with the correct path syntax, e.g.<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; &#8230; -&gt; &#8230;.<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; #&nbsp;&nbsp;&nbsp;&nbsp; &#8230;. -&gt; &#8230;&#8230;<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; $normalizedPath =<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ([regex]&#8221;.{3,}&#8221;).Replace($commandName, { (&#8216;..&#8217; * ($args[0].Length &#8211; 2) + &#8216;..&#8217;) })<b><\/b>\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; # If the command looks like a location, just switch to that directory<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; if (Test-Path -Path $normalizedPath)<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; {<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $eventArgs.CommandScriptBlock = { Set-Location -LiteralPath $normalizedPath }.GetNewClosure()<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; }<b><\/b>\n&nbsp;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; if ($commandName -ne $normalizedPath)<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; {<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Maybe the command is an external script\/native exe specified with &#8230;.<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $eventArgs.Command = Get-Command -Name $normalizedPath -ea Ignore<b><\/b><\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; }<b><\/b><\/p>\n<p style=\"margin-left:30px\">}\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 talks to members of the Windows PowerShell team about what is in their Windows PowerShell profile. Microsoft Scripting Guy, Ed Wilson, is here. I&#8217;m wrapping up the week with profile excerpts from the Windows PowerShell team. There is some really cool stuff here. Hemant Mahawar indicated that he does [&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-1302","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 talks to members of the Windows PowerShell team about what is in their Windows PowerShell profile. Microsoft Scripting Guy, Ed Wilson, is here. I&#8217;m wrapping up the week with profile excerpts from the Windows PowerShell team. There is some really cool stuff here. Hemant Mahawar indicated that he does [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1302","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=1302"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1302\/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=1302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}