How to check if a cmdlet exists in PowerShell at runtime via script
Solution 1:
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
Solution 2:
This is a simple function to do what you're like to do :)
function Check-Command($cmdname)
{
return [bool](Get-Command -Name $cmdname -ErrorAction SilentlyContinue)
}
How to use (for example):
if (Check-Command -cmdname 'Invoke-WebRequest')
{
Invoke-WebRequest $link -OutFile $destination
}
else
{
$webclient.DownloadFile($link, $destination)
}