I’d like a function to test for the existence of a command (cmdlet, function, alias, etc.) in PowerShell. It should behave like this:
PS C:\> Test-Command ls
True
PS C:\> Test-Command lss
False
I have a function that works but strikes me as neither idiomatic nor elegant. Is there a more posh way to do this:
function Test-Command( [string] $CommandName )
{
$ret = $false
try
{
$ret = @(Get-Command $CommandName -ErrorAction Stop).length -gt 0
}
catch
{
# do nothing
}
return $ret
}
Bonus question:
Python : pythonic :: PowerShell : ?
I’d say posh but is there something else in common use?
How about this:
(BTW, I like posh )