LOGO

Geek School: PowerShell Cmdlets - A Beginner's Guide

March 26, 2013
Geek School: PowerShell Cmdlets - A Beginner's Guide

PowerShell: Building on Command Prompt Familiarity

Individuals accustomed to utilizing commands like ipconfig or ping within the command prompt are already well-positioned to learn PowerShell. This suggests a foundational understanding that simplifies the transition to more advanced scripting capabilities.

This article serves as a continuation of our Geek School series, designed to guide readers through the intricacies of PowerShell.

A Natural Progression

The skills developed through command prompt usage provide a solid base for mastering PowerShell. The core concepts are similar, but PowerShell offers significantly expanded functionality.

We invite you to explore the world of cmdlets – the fundamental building blocks of PowerShell scripts – as we progress through this instructional series.

Stay Connected with Geek School

Ensure you have reviewed the introductory article to this PowerShell series for a comprehensive overview of its origins and capabilities.

Further installments of Geek School will be released throughout the week, providing continued learning opportunities and deeper dives into specific PowerShell features.

Understanding Cmdlet Structure

As discussed in the initial segment of this series, a typical PowerShell cmdlet often appears in a format similar to this:

Update-Help

PowerShell cmdlets adhere to a Verb-Noun naming convention, as demonstrated above. A key principle to remember is that the noun component is consistently singular, regardless of whether the cmdlet returns multiple items. You can discover a comprehensive list of approved verbs for use in PowerShell by utilizing the Get-Verb cmdlet.

geek-school-learning-to-use-cmdlets-in-powershell-1.jpg

Familiarity with the permissible verbs and the singular noun rule significantly aids in predicting cmdlet names. For instance, if the objective is to retrieve a listing of services and their current status, the cmdlet is straightforward: Get-Service. Considering this pattern, how would one obtain a list of currently running processes? Get-Process provides the solution.

Get-Process

geek-school-learning-to-use-cmdlets-in-powershell-2.jpg

This consistency simplifies interaction with diverse technologies. Should the Exchange cmdlets be loaded, acquiring a list of mailboxes on the server becomes easily achievable through:

Get-Mailbox

However, an exception exists. With the exception of Exchange, all other technology-specific commands necessitate a prefix. For example, to enumerate users currently connected via Remote Desktop, the command would be:

Get-RDUserSession

This is illustrated in the image below.

Please note: The screenshot was captured on a Server 2012 system, as this is where you'll typically find the majority of technology-specific modules.

geek-school-learning-to-use-cmdlets-in-powershell-3.jpg

I encountered an explanation from Don Jones, widely recognized as a leading PowerShell authority, detailing that Exchange predates the implementation of this prefixing convention for cmdlets, and consequently, it was never integrated and will not be in the future.

Aliases in PowerShell

PowerShell provides a useful feature allowing multiple commands to achieve the same result – these are known as aliases. A significant benefit is the inclusion of commands familiar to users of both the command prompt and Linux environments.

Listing Directory Contents

For instance, obtaining a directory listing in PowerShell can be accomplished using the following cmdlet:

Get-ChildItem

Users accustomed to the command prompt will find this convenient, as PowerShell offers compatibility with their existing workflows.

Linux Compatibility

Those with a Linux background will also discover familiar aliases readily available.

As scripting experience grows, developers often adopt aliases for efficiency. However, this practice can hinder understanding for those new to the code.

Discovering Underlying Commands

To determine the actual command executed by an alias, the following can be used:

Get-Alias –Name ls

This reveals the cmdlet that the alias represents, aiding in code comprehension.

Exploring Cmdlet Aliases

Alternatively, to view all aliases associated with a specific cmdlet, utilize the definition parameter:

Get-ChildItem –Definition Get-ChildItem

This provides a comprehensive list of aliases for the specified cmdlet.

Creating Custom Aliases

Users can extend PowerShell's functionality by defining their own aliases. This is achieved through the following command:

New-Alias –Name icanhazfilez –Value Get-ChildItem

Remember to substitute “icanhazfilez” with your desired alias name and “Get-ChildItem” with the corresponding cmdlet.

Alias Persistence

It’s important to note that defined aliases are lost upon shell closure. To maintain these aliases across sessions, their definitions must be added to your PowerShell profile script.

Parameter Abbreviation in Windows PowerShell

Windows PowerShell provides a feature enabling the shortening of parameter names. This functionality is permissible until the point of ambiguity arises. Ambiguity occurs when PowerShell is unable to discern the intended parameter.

For instance, consider the following command:

Get-Service -Name 'Apple Mobile Device' -ComputerName localhost

This command is functionally equivalent to:

Get-Service -Na 'Apple Mobile Device' -Com localhost

The ability to truncate parameter names streamlines command entry, enhancing efficiency.

However, it's crucial to avoid excessive abbreviation.

Should the parameter names become overly ambiguous, PowerShell will return an error. This indicates that the shortened names are insufficient for proper interpretation.

Important Note: Carefully balance brevity with clarity when utilizing parameter truncation to ensure successful command execution.

Continued Functionality of Existing Commands

PowerShell maintains compatibility with commands familiar to users. Existing commands will continue to operate as expected.

ping www.google.com

It’s important to note that traditional applications, such as the ping command, generate string-based output.

However, PowerShell often provides more efficient alternatives through its native cmdlets.

geek-school-learning-to-use-cmdlets-in-powershell-12.jpg

While legacy commands still function, PowerShell cmdlets offer a different approach to data handling.

Instead of receiving extensive text strings, the output is structured as an object. This object-oriented approach will be explored further in the next installment of Geek School.

geek-school-learning-to-use-cmdlets-in-powershell-13.jpg

The shift to objects allows for more refined manipulation and analysis of data within the PowerShell environment.

#PowerShell#cmdlets#Geek School#PowerShell tutorial#PowerShell for beginners#scripting