Skip to content

Instantly share code, notes, and snippets.

@Trucido
Last active March 17, 2019 05:33
Show Gist options
  • Select an option

  • Save Trucido/dff57747efc3f4b0f9d72e6b4a3c9903 to your computer and use it in GitHub Desktop.

Select an option

Save Trucido/dff57747efc3f4b0f9d72e6b4a3c9903 to your computer and use it in GitHub Desktop.
function which2 {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
$Name
)
$ErrorActionPreference = 'Stop'
try {
[object]$getCommand = Get-Command $Name
[object]$commandType = $getCommand.CommandType
switch ($commandType)
{
Application { return ($getCommand | Format-List) }
Alias { try {
$gcmGcm = (Get-Command $($getCommand))
$gcmGcmDef = (Get-Command $($gcmGcm.Definition))
$gcmGcmType = (Get-Command $($gcmGcm.Definition)).CommandType
if ($gcmGcmType -eq 'ExternalScript') {
return ($gcmGcmDef | Format-List -Property Source,ModuleName,CommandType,ScriptContents,Definition)
} else {
return ($gcmGcmdef | Format-List -Property Source,ModuleName,CommandType,Definition,DisplayName)
}
} catch {
return ($getCommand | Format-List -Property Source,CommandType,Definition,DisplayName)
}
}
ExternalScript { return ($getCommand | Format-List -Property Source,ModuleName,CommandType,ScriptContents,Definition) }
Default { return ($getCommand | Format-List -Property Source,ModuleName,CommandType,Definition) }
}
}
catch [System.Management.Automation.CommandNotFoundException] {
$ErrorActionPreference = (-not($PSBoundParameters.ContainsKey('ErrorAction') -and ($PSBoundParameters['ErrorAction'] -ne $false)))
$InformationPreference = (-not($PSBoundParameters.ContainsKey('InformationAction') -and ($PSBoundParameters['InformationAction'] -ne $false)))
if ($wherecmd = $(where.exe *$Name* 2>$null)) {
if (($null -ne $whereCmd) -and ($whereCmd.Count -ne 0)) {
return "Command ${Name} not found, but there are $($whereCmd.Count) similar ones"
}
}
return "${Name}: command not found"
}
catch [System.Management.Automation.RuntimeException],[System.InvalidOperationException],
[System.Management.Automation.PSArgumentException] {
if ($null -ne $getCommand) {
$Error.Remove($error[0])
$getcmdExpand = ($getCommand | Format-List -Expand Both)
return $getcmdExpand
}
}
catch {
return "${Name}: command not found"
}
# .FORWARDHELPTARGETNAME Get-Command
# .FORWARDHELPCATEGORY Cmdlet
}
if (!($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq '')) {
which2 @args
} # else we're being dot-sourced, do nothing.
# Local Variables:
# mode: PowerShell; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4;
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment