PowerShell成语测试命令的存在?

jwf*_*arn 3 powershell idioms idiomatic

我想要一个函数来测试PowerShell中是否存在命令(cmdlet,函数,别名等).它应该像这样:

PS C:\> Test-Command ls
True
PS C:\> Test-Command lss
False
Run Code Online (Sandbox Code Playgroud)

我有一个有效的功能,但我既不是惯用也不是优雅.有没有更豪华的方式来做到这一点:

function Test-Command( [string] $CommandName )
{
    $ret = $false
    try
    {
        $ret = @(Get-Command $CommandName -ErrorAction Stop).length -gt 0
    }
    catch
    {
        # do nothing
    }
    return $ret
}
Run Code Online (Sandbox Code Playgroud)

奖金问题:

Python:pythonic :: PowerShell : ?

我会说豪华,但还有其他常用的东西吗?

zda*_*dan 6

这个怎么样:

function Test-Command( [string] $CommandName )
{
    (Get-Command $CommandName -ErrorAction SilentlyContinue) -ne $null
}
Run Code Online (Sandbox Code Playgroud)

(顺便说一句,我喜欢豪华)