Home » How to Use the Find-Command Cmdlet in Powershell

How to Use the Find-Command Cmdlet in Powershell

The Find-Command cmdlet in PowerShell is a powerful tool that helps locate cmdlets, aliases, functions, and workflows across registered repositories.

The Find-Command cmdlet returns a PSGetCommandInfo object for each command found.

The Find-Command uses the Name parameter that specifies the name of the command and locates the modules in the repository. It returns the multiple modules PowerShellGet and CompatPowerShellGet.

PowerShell Find-Command
PowerShell Find-Command

In this article, we will discuss how to:

  • Find all commands in a repository.
  • Find commands by name.

PowerShell Find-Command

The Find-Command cmdlet searches for PowerShell commands within available modules. It uses the Name parameter to locate specific commands and provides details about the associated module.

Syntax

Find-Command
    [[-Name] <String[]>]
    [-ModuleName <String>]
    [-MinimumVersion <String>]
    [-MaximumVersion <String>]
    [-RequiredVersion <String>]
    [-AllVersions]
    [-AllowPrerelease]
    [-Tag <String[]>]
    [-Filter <String>]
    [-Proxy <Uri>]
    [-ProxyCredential <PSCredential>]
    [-Repository <String[]>]
    [<CommonParameters>]

Let’s understand the Find-Command in PowerShell with the help of examples.

Find all Commands in a Repository

To discover commands available in a specific repository, such as PSGallery, use the following command:

Find-Command -Repository PSGallery | Select-Object -First 10

In this example:

  • Find-Command -Repository PSGallery : Searches PSGallery for available commands.
  • Select-Object -First 10 : Limits the output to the first 10 results.

The output of the above PowerShell script to find commands in the specified repository returns the commands as follows.

Find all Commands in a Repository
Find all Commands in a Repository

Find and Install a Command by Name

To locate a specific command and install the module containing it, use the Name and ModuleName parameters:

Find-Command -Name Split-File -Repository PSGallery -ModuleName FileSplitter | Install-Module

In the above PowerShell script:

  • Find-Command -Name Split-File locates the Split-File command.
  • The Repository parameter restricts the search to the PSGallery repository.
  • The ModuleName parameter specifies the FileSplitter module.
  • The result is piped to Install-Module which installs the specified module.
Get-InstalledModule 

The output of the above PowerShell script to locate the command, module, and install the module is:

PowerShell Find-Command location and Install
PowerShell Find-Command location and Install

Find Command by Name Only

To locate a command by name without installing it, use the Name parameter:

Find-Command -Name Get-FileVersion

In the above PowerShell script:

  • Uses Find-Command to search for Get-FileVersion.
  • Returns all modules that include Get-FileVersion.

The output of the above PowerShell script returns the module names for the specified command name.

PS C:\> Find-Command -Name Get-FileVersion                                                                              
Name                                Version    ModuleName                          Repository
----                                -------    ----------                          ----------
Get-FileVersion                     1.0.29     PSSoftware                          PSGallery
Get-FileVersion                     3.8.4.1    PSADT                               PSGallery
Get-FileVersion                     1.1.3.1    HostUtilities                       PSGallery
Get-FileVersion                     1.5        Get-FileVersion                     PSGallery
Get-FileVersion                     1.0.0.0    SoftwareInstallManager              PSGallery
Get-FileVersion                     1.1.0.0    BAMCIS.FileIO                       PSGallery
Get-FileVersion                     1.6.6      BUILDLet.PowerShell.Utilities       PSGallery

Conclusion

I hope the above article on how to use Find-Command in PowerShell is helpful to you.

If the command name exists in multiple modules, use the ModuleName parameter to specify the module for installation.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.