Introduction
Command Naming with Verbs and Nouns
Commands in PowerShell are structured around verb and noun pairs, which allow for intuitive and meaningful command names. The list of approved verbs is maintained by Microsoft and includes common words such as Add, Get, Set, and New. This standardized approach to naming commands greatly aids in command discovery and reduces the need to memorize lengthy lists of commands.
Viewing Available Verbs
To view the available verbs in PowerShell, you can use the following command:
Get-Verb
This command will provide you with a list of approved verbs, categorized into different areas such as data, life cycle, and security. Understanding the verbs and their associated areas can help you make educated guesses when searching for specific commands.
Importance of Verb-Noun Pairing
The verb-noun pairing in command naming is designed to provide a clear indication of the action performed by the command. For example, complementary actions like encryption and decryption often use verbs from the same group. For instance, the verb "Protect" may be used to encrypt something, while the verb "Unprotect" may be used to decrypt it. This consistency in naming conventions enhances the discoverability and logical organization of commands.
Caution with Unapproved Verbs
While it's possible to use verbs other than those in the approved list, it is generally not recommended. If a command with an unapproved verb is included in a module, a warning message will be displayed every time the module is imported. It's best to adhere to the approved verb list to maintain consistency and avoid potential conflicts.
Utilizing Nouns in Command Naming
In addition to verbs, nouns play a vital role in command naming. A noun provides a concise description of the object or entity the command is expected to act upon. Nouns can be single words like "Process," "Item," or "Help," or they can be multi-word phrases like "ChildItem," "WebRequest," or "MailMessage." The noun in a command name gives users a clear idea of the target or subject of the command.
Command Name Prefixes
Command names often include a prefix on the noun to indicate the module or context to which the command belongs. For example, the command "Get-ADUser" is part of the ActiveDirectory module, and all commands in that module use "AD" as a prefix for the noun. Modules are further explored in Chapter 2, Modules and Snap-Ins.
Efficient Command Discovery
The verb-noun pairing in PowerShell commands aims to make command discovery easier without relying heavily on search engines. By understanding the command naming convention and utilizing wildcard characters, you can quickly identify relevant commands for your specific needs.
Using Wildcards in Command Discovery
Let's say you want to list firewall rules, but you're not sure which command to use. You can use the Get-Command cmdlet with a wildcard search to find the relevant commands. For example:
Get-Command Get-*Firewall*
This command will provide you with a list of "Get" commands that might affect the firewall. The list of commands returned may vary depending on the modules installed on your computer. From the list, you can identify the command that closely matches your requirement, such as "Get-NetFirewallRule," and further explore its capabilities.
Assessing Command Suitability with Get-Help
Once you have identified a potential command, you can use the Get-Help cmdlet to assess whether the command is suitable for your task. Get-Help provides detailed documentation and usage examples for each command. For example:
Get-Help Get-NetFirewallRule
This command will display the comprehensive help content for the "Get-NetFirewallRule" command, including its syntax, parameters, usage examples, and related links. By leveraging the information provided by Get-Help, you can gain a deeper understanding of the command's capabilities and make informed decisions.
Understanding Aliases in PowerShell
In addition to the standard command names, PowerShell also supports aliases, which are alternate names for commands. Aliases can be helpful for shortening command names or providing more familiar command names for users coming from other command-line environments. However, the use of aliases in production code is generally discouraged.
Viewing and Using Aliases
To view the list of aliases in PowerShell, you can use the following command:
Get-Alias
This command will display a list of aliases, including their corresponding command names. For example, some commonly used aliases are "%," which is an alias for "ForEach-Object," and "?" for "Where-Object."
You can also use Get-Alias to find the command behind an alias. For instance:
Get-Alias dir
This command will reveal the command behind the "dir" alias, which is "Get-ChildItem." Similarly, you can find the aliases for a specific command using the -Definition parameter:
Get-Alias -Definition Get-ChildItem
This command will display the aliases associated with the "Get-ChildItem" command, such as "dir," "gci," and "ls."
Creating and Removing Aliases
New aliases can be created using the New-Alias command. For example, to create an alias named "grep" for the "Select-String" command, you can use the following command:
New-Alias grep -Value Select-String
To remove an alias, you can use either the Remove-Alias command or Remove-Item as an alternative. For example:
Remove-Alias grep
Remove-Item alias:\grep
It's important to note that aliases created in one PowerShell session are not remembered when a new session is started.
Conclusion
Command naming and discovery are essential aspects of PowerShell that can greatly enhance the productivity and efficiency of intermediate users. By understanding the verb-noun pairing, exploring available verbs, utilizing nouns, and leveraging aliases, users can quickly discover and utilize the appropriate commands for their tasks. With the knowledge gained from this comprehensive guide, intermediate users can harness the power of PowerShell to streamline their automation and management workflows and achieve greater productivity in their daily tasks.
For a detailed list of approved verbs and their use cases, you can refer to the official Microsoft documentation on approved verbs for Windows PowerShell commands: Approved Verbs for Windows PowerShell Commands.
Pointer version below:
Command Naming with Verbs and Nouns
PowerShell commands are structured as verb-noun pairs.
The list of approved verbs, maintained by Microsoft, includes Add, Get, Set, and New.
Approved verbs are categorized into areas such as data, life cycle, and security.
Verbs from the same group are often used for complementary actions.
Using unapproved verbs is generally not recommended.
Viewing Available Verbs
To view the available verbs in PowerShell, use the command: Get-Verb.
Utilizing Nouns in Command Naming
Nouns provide a concise description of the object the command acts upon.
Nouns can be single words or multi-word phrases.
Command names may include a module-specific prefix on the noun.
Efficient Command Discovery
The verb-noun pairing simplifies command discovery without relying heavily on search engines.
Wildcard characters can be used to search for relevant commands.
Example: Get-Command Get-*Firewall* to list firewall-related commands.
Assessing Command Suitability with Get-Help
Use the Get-Help cmdlet to assess a command's suitability.
Get-Help provides detailed documentation, syntax, parameters, and usage examples.
Example: Get-Help Get-NetFirewallRule.
Understanding Aliases in PowerShell
Aliases provide alternate names for commands.
They can help shorten command names or offer familiarity from other command-line environments.
Production use of aliases is generally discouraged.
Viewing and Using Aliases
Use Get-Alias to view the list of aliases and their corresponding command names.
Creating and Removing Aliases
New aliases can be created with the New-Alias command.
Aliases can be removed using Remove-Alias or Remove-Item.
Additional Technical Information
PowerShell supports common aliases like % for ForEach-Object, ? for Where-Object, cd for Set-Location, and ls or dir for Get-ChildItem.
Aliases can be created and removed within a PowerShell session but are not remembered across sessions.
For a detailed list of approved verbs and their use cases, refer to the official Microsoft documentation on Approved Verbs for Windows PowerShell Commands.