What’s in Your PowerShell Profile? Powershell Team Favorites

Doctor Scripto

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’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 not use a profile. This 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. Lee Holmes shared a couple of really cool techniques. “Here are two killer techniques I use in my profile.”

Use PSDefaultParameterValues on Out-Default

“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 $0).”

123 [C:windowssystem32]

>> $PSDefaultParameterValues[“Out-Default:OutVariable”] = “0”  

124 [C:windowssystem32]

>> Get-Command Watch-Clipboard  

CommandType     Name                                               Version    Source

———–     —-                                               ——-    ——

ExternalScript  Watch-Clipboard.ps1                                           d:documentstoolsWatch-Clipboard.ps1  

125 [C:windowssystem32]

>> ise $0.Source

Auto-register scheduled jobs

“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.”

if(-not (Get-ScheduledJob -Name PowerFix -EA Ignore))

{

    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration ([TimeSpan]::MaxValue)

    $null = Register-ScheduledJob -Name PowerFix -Trigger $trigger -ScriptBlock { powercfg -x -standby-timeout-ac 0 } -MaxResultCount 1

} Steve Lee is a principal test lead in Windows PowerShell and WMI, and he provided links to several resources.

Abhik Chatterjee provided a useful function to use when you are dealing with nested directories: “I don’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.”

function prompt

{

            Write-Host(“PS: ” + “$pwd” + “>”)

} Jason Shirk offered the following suggestion for changing directories. “Here is a ‘command not found’ handler that I use to change directories without typing cd.”

 $ExecutionContext.InvokeCommand.CommandNotFoundAction =

{

    param([string]$commandName,

          [System.Management.Automation.CommandLookupEventArgs]$eventArgs)  

    # Remove the ‘get-‘ prefix that confuses Test-Path after we produce

    # something that is path like after the get-.

    if ($commandName.StartsWith(‘get-‘))

    {

        $commandName = $commandName.Substring(4)

    }  

    # Replace sequences of 3 or more dots with the correct path syntax, e.g.

    #     … -> ….

    #     …. -> ……

    $normalizedPath =

        ([regex]”.{3,}”).Replace($commandName, { (‘..’ * ($args[0].Length – 2) + ‘..’) })  

    # If the command looks like a location, just switch to that directory

    if (Test-Path -Path $normalizedPath)

    {

        $eventArgs.CommandScriptBlock = { Set-Location -LiteralPath $normalizedPath }.GetNewClosure()

        return

    }  

    if ($commandName -ne $normalizedPath)

    {

        # Maybe the command is an external script/native exe specified with ….

        $eventArgs.Command = Get-Command -Name $normalizedPath -ea Ignore

    }

} I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon