Skip to content

Instantly share code, notes, and snippets.

@Pagliacii
Last active February 21, 2021 14:53
Show Gist options
  • Save Pagliacii/55b9e9b43bf662a58a8340d130cd862c to your computer and use it in GitHub Desktop.
Save Pagliacii/55b9e9b43bf662a58a8340d130cd862c to your computer and use it in GitHub Desktop.
A which command implementation in PowerShell
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