Test if executable is in path in PowerShell

前端 未结 2 1552
一生所求
一生所求 2020-12-28 11:25

In my script I\'m about to run a command

pandoc -Ss readme.txt -o readme.html

But I\'m not sure if pandoc is installed. So I w

2条回答
  •  眼角桃花
    2020-12-28 12:00

    You can test through Get-Command (gcm)

    if (Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) 
    { 
       pandoc -Ss readme.txt -o readme.html
    }
    

    If you'd like to test the non-existence of a command in your path, for example to show an error message or download the executable (think NuGet):

    if ((Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) -eq $null) 
    { 
       Write-Host "Unable to find pandoc.exe in your PATH"
    }
    

    Try

    (Get-Help gcm).description
    

    in a PowerShell session to get information about Get-Command.

提交回复
热议问题