Last active
February 21, 2021 14:53
-
-
Save Pagliacii/55b9e9b43bf662a58a8340d130cd862c to your computer and use it in GitHub Desktop.
A which command implementation in PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function WhichCommand( | |
[String]$cmd | |
) { | |
try { | |
$Command = Get-Command -ErrorAction Stop $cmd | |
} | |
catch { | |
Write-Host -ForegroundColor Red ("{0} not found" -f $cmd) | |
Return | |
} | |
if ($Command.CommandType -eq "Alias") { | |
Write-Host ("{0}: aliases to {1}" -f $cmd, $Command.Definition.Trim()) | |
} | |
elseif ($Command.CommandType -eq "Function") { | |
Write-Host ("{0}: Function {{{1}}}" -f $cmd, $Command.Definition) | |
} | |
elseif ($Command.CommandType -eq "Cmdlet") { | |
Write-Host ("{0}: a Cmdlet in {1}" -f $cmd, $Command.Source.Trim()) | |
} | |
else { | |
Write-Host $Command.Source.Trim() | |
} | |
} | |
Set-Alias which WhichCommand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment