Test Whether Applications Exist

by Oct 21, 2020

Here is a simple one-liner that can test whether PowerShell 7 is installed on your system (or any other application):

# name of application you want to test
$name = 'pwsh'

# try and find the application. Discard result and errors.
Get-Command -Name $name -ErrorAction Ignore | Out-Null

# if there was an error, the application does not exist
$exists = $?

# output result
"Does $name exist? $exists"

In essence, the code uses Get-Command to check the application by name. Any application that is installed in one of the folders listed in $env:path will be recognized.

The code above would return $true if PowerShell 7 was installed in one of the default folders listed in $env:path. It would return $false if PowerShell 7 was either not yet installed or installed in some private folder that is not discoverable by Get-Command.

Twitter This Tip! ReTweet this Tip!