Introduction:
PowerShell is a powerful automation and scripting language that offers a plethora of commands and functions. Among these, the Get-Command
cmdlet stands out as a versatile tool that helps sysadmins discover, explore, and interact with all available commands. In this blog post, we’ll take a deep dive into the Get-Command
cmdlet, exploring its features and providing detailed code examples to help you harness its full potential.
What is Get-Command?
The Get-Command
cmdlet in PowerShell is used to retrieve information about available commands within the current session. It can be employed to discover cmdlets, functions, scripts, and executable files. Understanding how to use Get-Command
effectively is essential for sysadmins, as it allows you to tap into the vast repository of PowerShell commands at your disposal.
Basic Usage of Get-Command:
Syntax:
Get-Command [-Name] <String> [-Verb <String>] [-Noun <String>] [-Module <String[]>] [-CommandType <CmdletType>]
Name
: Specifies the name of the command you want to retrieve.Verb
: Filters commands based on the verb (e.g.,Get
,New
,Set
).Noun
: Filters commands based on the noun (e.g.,Process
,Service
,Item
).Module
: Limits the search to specific modules.CommandType
: Filters commands based on their type (Cmdlet, Function, Script, Application, ExternalScript).
Practical Code Examples:
Example 1: Listing All Available Commands
Get-Command
This command will return a list of all available cmdlets, functions, and scripts in your PowerShell session.
Example 2: Finding Commands by Verb
Get-Command -Verb Get
This will display a list of all commands with the verb “Get,” helping you quickly find retrieval commands.
Example 3: Finding Commands by Noun
Get-Command -Noun Service
This will display a list of all options with the Noun “Service,” helping you quickly find retrieval commands.
Example 4: Searching for a Specific Command
Get-Command -Name Get-Service
This command provides information about the “Get-Service” cmdlet, including its name, definition, and module.
Example 5: Filtering by Module
Get-Command -Module ActiveDirectory
This retrieves all commands that belong to the “ActiveDirectory” module.
Example 6: Differentiating Command Types
Get-Command -CommandType Function
Here, we filter and list only functions available in your session.
Advanced Techniques:
Discovering Command Aliases
Get-Command -Name gci
This example reveals that the “gci” command is an alias for “Get-ChildItem.”
Sorting and Formatting
Get-Command | Sort-Object Name | Format-Table Name, CommandType -AutoSize
This code sorts the commands alphabetically and formats them into a table, displaying only the name and command type.
Conclusion:
The Get-Command
cmdlet in PowerShell is a fundamental tool for sysadmins and anyone working with PowerShell. It empowers you to explore and understand the available commands in your environment, enabling more efficient scripting and automation. By mastering the usage of Get-Command
with the provided code examples, you can become a more proficient PowerShell user and sysadmin.
In future blog posts, we’ll delve deeper into other PowerShell cmdlets and provide practical use cases to enhance your skills as a sysadmin.
Remember, practice makes perfect. Experiment with Get-Command
and discover the wealth of PowerShell commands at your disposal.