Skip to content

[Feature] Implement Test-Command cmdlet #8755

Closed

Description

@xtqqczze

Summary

A Test-Command cmdlet should be implemented to test whether or not a specified command can be found.

Context

As per issue #7562, more Test- cmdlets should be implemented, specifically Test-Command and Test-Module, as this functionality is desired both generally and for use in the PowerShell tests and build module.

Examples

PS C:\> Test-Command -Name pwsh -CommandType Application
True
PS C:\> Test-Command -Name doesnotexist -CommandType All
False
PS C:\> Test-Command -Name Get-NonexistentFeature, Get-Help -CommandType Cmdlet
False
True

Syntax

Test-Command [-Name] <string[]> [-CommandType <CommandTypes>] [<CommonParameters>]

Activity

  1. xtqqczze commented on Jan 27, 2019

    @xtqqczze
    ContributorAuthor

    Parameters should be named in common with Get-Command.

    Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>] [-Module <string[]>] [-FullyQualifiedModule <ModuleSpecification[]>] [-CommandType <CommandTypes>] [-TotalCount <int>] [-Syntax] [-ShowCommandInfo] [-All] [-ListImported] [-ParameterName <string[]>] [-ParameterType <PSTypeName[]>] [<CommonParameters>]
    
  2. iSazonov commented on Jan 28, 2019

    @iSazonov
    Collaborator

    I think it's not worth the effort. You can always use Get-Command cmdlet for these purposes.

  3. xtqqczze commented on Jan 28, 2019

    @xtqqczze
    ContributorAuthor

    Ilya (@iSazonov) A common pattern is to use the Get-Command cmdlet with the ErrorAction parameter set to SilentlyContinue, although this hides all errors:

    [bool](Get-Command -Name git -CommandType Application -ErrorAction SilentlyContinue)

    Alternatively, the specific exception can be caught:

    try {
        [bool](Get-Command -Name git -CommandType Application -ErrorAction Stop)
    }
    catch [System.Management.Automation.CommandNotFoundException] {
        $false
    }
  4. iSazonov commented on Feb 2, 2019

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) My suggestion is to close as Won't-Fix - what is your conclusion?

  5. SteveL-MSFT commented on Feb 6, 2019

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) well, if you look at the original linked issue #7562, I opened it :)

    The discussion in the @PowerShell/powershell-committee on a different topic acknowledged that PowerShell should have had more Test-* cmdlets rather than having scripts rely on Get-* with error handling and checking against $null. Also, with more Test-* cmdlets, it would make it feasible to use cmdlets directly as DSC resources (Get/Set/Test). It's unlikely the PowerShell team would spend the time implementing this. However, this specific cmdlet should be relatively easy and straightforward and would encourage the community to pick it up.

  6. iSazonov commented on Feb 6, 2019

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) Thanks for remainder! So the issue is a duplicate and we can track in #7562.

  7. JamesWTruher commented on Jun 1, 2021

    @JamesWTruher
    Collaborator

    duplicate of #7562, this is a great discussion with some good detail. We'll refer to it from #7562 when it's time for implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions