I need a function to check for the presence of a command (cmdlet, function, alias, etc.) in PowerShell. It should behave as follows:
PS C:\> Test-Command ls
True
PS C:\> Test-Command lss
False
I have a function that works, but it strikes me as neither idiomatic nor elegant. Is there a smarter way to do this:
function Test-Command( [string] $CommandName )
{
$ret = $false
try
{
$ret = @(Get-Command $CommandName -ErrorAction Stop).length -gt 0
}
catch
{
}
return $ret
}
Bonus question:
Python: pythonic :: PowerShell :?
I would say chic, but is there anything else in common use?
source
share