ProductivityTools.PSTestCommandExists.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

function Test-CommandExists {
    [CmdletBinding()]
    param($Name)

    $oldPreference = $ErrorActionPreference
    Write-Verbose "Previous ErrorActionPreference: $ErrorActionPreference"
    $ErrorActionPreference = "Stop"
    Write-Verbose "Set ErrorActionPreference to 'Stop'"

    try {
        $command=Get-Command $Name
        Write-Verbose "Command Exists $command"
        return $true
    }
    catch {
        Write-Verbose "Command $Name does not exist" 
        return $false
    }
    finally {
        $ErrorActionPreference=$oldPreference
        Write-Verbose "Restore ErrorActionPreference: $ErrorActionPreference"
    }
}

Export-ModuleMember Test-CommandExists