PoshCode Logo PowerShell Code Repository

Out-Default URL Launcher (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/803"></script>download | new post

This bad boy is from an email by Bruce Payette—the master. Here are some notes from the email:

“Add the function below to your profile and you should have what you want. By overriding Out-Default,
this function can do a couple of things. First, it captures the last object written in
$GLOBAL:LAST. The more germane feature is that it traps any errors that are written. If the error is
“command not found” and the command takes the form of “http://msdn.microsoft.com” or “live.com” it
uses [diagnostics.process]::start() to launch the browser (this could just be start-process in V2). Finally,
if the command name resolves to a directory, it cd’s to that directory.

This works in both the console host and the ISE but the implementation depends on our host convention – that
all output and errors are routed through Out-Default, so it may not work in other hosts.” – Bruce Payette

  1. # Wrapper for Out-Default that saves the last object written
  2. # and handles missing commands if the command is a directory
  3. # or an URL
  4. #
  5. function Out-Default `
  6. {
  7.    [CmdletBinding(ConfirmImpact="Medium")]
  8.    param(
  9.        [Parameter(ValueFromPipeline=$true)] `
  10.        [System.Management.Automation.PSObject] $InputObject
  11.    )
  12.  
  13.    begin
  14.    {
  15.      $wrappedCmdlet = $ExecutionContext.InvokeCommand.GetCmdlet(
  16.        "Out-Default")
  17.      $sb = { & $wrappedCmdlet @PSBoundParameters }
  18.      $__sp = $sb.GetSteppablePipeline()
  19.      $__sp.Begin($pscmdlet)
  20.    }
  21.    process {
  22.  
  23.        $do_process = $true
  24.        if ($_ -is [System.Management.Automation.ErrorRecord])
  25.        {
  26.            if ($_.Exception -is [System.Management.Automation.CommandNotFoundException])
  27.            {
  28.                $__command = $_.Exception.CommandName
  29.                if (test-path -path $__command -pathtype container)
  30.                {
  31.                    set-location $__command
  32.                    $do_process = $false
  33.                }
  34.                elseif ($__command -match '^http://|\.(com|org|net|edu)$')
  35.                {
  36.                    if ($matches[0] -ne "http://") { $__command = "HTTP://" + $__command }
  37.                    [diagnostics.process]::Start($__command)
  38.                    $do_process = $false
  39.                }
  40.            }
  41.        }
  42.        if ($do_process)
  43.        {
  44.            $global:LAST = $_;
  45.            $__sp.Process($_)
  46.        }
  47.    }
  48.    end { $__sp.End() }
  49. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me