Home » How to use Find Command in Powershell

How to use Find Command in Powershell

The Find-Command cmdlet in PowerShell finds the cmdlets, aliases, functions, and workflows. Find-Command cmdlet searches a registered repository for the modules.

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

Find-Command uses the Name parameter for 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 use Find-Command in PowerShell to find all commands in the repository, and find commands by name.

PowerShell Find-Command

It finds PowerShell commands in the modules and returns the PSGetCommandInfo object for each command. PSGetCommandInfo object can be piped to the Install-Module cmdlet to install the module that contains the command.

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 the Repository

To find all commands in the specified repository, use the following command.

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

In the above PowerShell script, the Find-Command cmdlet searches the modules in the central repository PSGallery. It returns objects which are piped to Select-Object.

Select-Object uses the First parameter to display the first 10 results.

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

Find Command in PowerShell
Find Command in PowerShell

How to Find Commands by Name and Install the Module

The Find-Command in PowerShell finds the location of the command and module. Find-Command uses the Name parameter to specify the command and the ModuleName parameter to specify the module name.

In the following example, Find-Command locates the command and module and uses Install-Module to install the module.

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

In the above PowerShell script, Find-Command uses the Name parameter to specify the command name Split-File. The Repository parameter searches the modules in the central repository of the PowerShell content PSGallery.

ModuleName parameter is used to specify the module name you want to install, FileSplitter. The object will be piped to the Install-Module cmdlet to install the module and its commands.

To confirm the successful installation of the PowerShell module, use the Get-InstalledModule to display the results.

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

How to Find Command by Name

Using the Find-Command, you can find the command by its name. The Find-Command uses the command name to locate the module in a Repository.

Find-Command -Name Get-FileVersion

In the above PowerShell script, we have provided the command name as Get-FileVersion. Find-Command uses the name of the command Get-FileVersion to locate the module in a repository.

It might be possible that the command name exists in multiple modules.

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.