How to check if a cmdlet exists in PowerShell at runtime via script

前端 未结 3 1577
感情败类
感情败类 2021-02-02 05:35

I have a PowerShell script that needs to run under multiple hosts (PowerGUI, PowerShell ISE, etc...), but I am having an issue where sometimes a cmdlet doesn\'t exist under one

3条回答
  •  隐瞒了意图╮
    2021-02-02 06:00

    Use the Get-Command cmdlet to test for the existence of a cmdlet:

    if (Get-Command $cmdName -errorAction SilentlyContinue)
    {
        "$cmdName exists"
    }
    

    And if you want to ensure it is a cmdlet (and not an exe or function or script) use the -CommandType parameter e.g -CommandType Cmdlet

提交回复
热议问题