Getty Images/iStockphoto

Tip

Use these PowerShell add-ons to supercharge your experience

These PowerShell new features and modules introduce several customization and functionality features to the popular automation tool to ease coding tasks for administrators.

The developers behind PowerShell released a handful of tools and new functionality to improve how you work with the popular administrative tool.

The extensibility aspect of PowerShell is among its many strengths. These PowerShell add-ons can help you be more efficient and offer many customization options. The new features range from user-interface tweaks, such as adding color, to opening up the PowerShell prompt to the artificial intelligence capabilities of the Azure cloud for easier script development.

The current state of PowerShell

PowerShell has been at version 7 for more than a year, and it's time to leave Windows PowerShell behind for several reasons, but mainly because any new features are only available in PowerShell v7.

PowerShell v5 is known as Windows PowerShell. It is no longer under development, but Microsoft will include it in future versions of Windows. The company will only provide security updates for Windows PowerShell.

PowerShell v7 runs on Windows, Mac and Linux; it uses a cross-platform version of the .Net software framework -- formerly .Net Core -- to make it work on those platforms. Microsoft initially referred to this open source version as PowerShell Core but has since reclaimed the PowerShell name.

As of publication, PowerShell v7 is still an add-on that you manually install in new Windows builds. One of the recent PowerShell releases introduced a Microsoft Update feature to deliver  new releases via conventional methods, such as Windows Update for Business and Windows Server Update Services.

How to work with ANSI colors and the $PSStyle variable

At time of publication, the latest production-ready iteration of PowerShell is v7.2.2. This version is stable and ready to use on corporate workstations.

In PowerShell v7.2, Microsoft introduced an automatic variable named $PSStyle to go with the support for ANSI colors for text. This feature benefits admins who want to customize the console colors to their liking.

PowerShell ANSI support
Microsoft added support for ANSI colors in PowerShell v7.2 for admins who want to add color to certain areas, such as green for headers.

While the PowerShell team introduced support for ANSI colors, they did not include any new cmdlets to configure the colors. You adjust the $PSStyle variable to change the colors. The variable has 26 color values.

$PSStyle properties
The $PSStyle automatic variable shows the properties for the text color on the PowerShell console.

Initially, it might look difficult to work with the values, but it gets easier once you see the pattern. The color code chart below shows 16 colors to get you started.

Color Value Color Value

Black

30

Bright Black

30;1

Red

31

Bright Red

31;1

Green

32

Bright Green

32;1

Yellow

33

Bright Yellow

33;1

Blue

34

Bright Blue

34;1

Magenta

35

Bright Magenta

35;1

Cyan

36

Bright Cyan

36;1

White

37

Bright White

37;1

There are two ways to set a value to change the colors: Use the color name or the color value.

The following example changes the table headers to Bright Red.

$PSStyle.Formatting.TableHeader = $psstyle.Foreground.BrightRed

$PSStyle variable color change
Set the table headers in the PowerShell console from Bright Green to Bright Red by adjusting the $PSStyle variable.

The second option to set the color value is to use the actual value of the color. The following example changes the output of the table header from Bright Green to Bright Yellow.

$PSStyle.Formatting.TableHeader = "`e[33;1m"

$PSStyle table header color
Use $PSStyle to change the table header from Bright Green to Bright Yellow.

The changes only live in the current session. When the PowerShell console closes, the colors revert to their default. Permanent color changes must be added to your PowerShell profile script.

How to use the PSReadLine module

The PSReadLine module customizes the command-line editing environment in PowerShell with several features, including syntax highlighting, multi-line editing and support for undo-redo. The module is several years old, but it was recently rewritten to work with the cross-platform PowerShell v7, introducing new functionality in the process.

To start, run the following command to install the module:

Install-Module PSReadLine

After installation, syntax coloring turns on by default.

PSReadLine module colors
The PSReadLine module provides customization of specific properties in the PowerShell console. In this example, the cmdlet name is yellow and the command parameters are gray.

The PSReadLine history feature gives access to a list of previously used commands. The module stores all the code run from PowerShell sessions in a central file on the host machine to predict code you might enter next.

PSReadLine history feature
The PSReadLine history feature suggests previously used code in dark gray.

You can customize the colors to something that makes working in sessions easier. The color options are hexadecimal, RGB or console colors. One line of code sets the colors to your preferences. The following command sets the text in red -- using a #ff0000 hexadecimal value -- for matches in search history and cyan for the predictive suggestion.

Set-PSReadLineOption -Colors @{emphasis = '#ff0000'; inlinePrediction = 'Cyan'}

To show how useful color customization is, press the F2 to toggle the view style from inline view to list view.

PSReadLine list view
The F2 key-binding in PSReadLine toggles the inline view to list view to show the last 10 matches from the command history.

Dynamic Help is another useful PSReadLine feature. Type the name of the cmdlet and press F1 for the cmdlet's help. Prior to PSReadLine, you would type Get-Help, or its alias Help, followed by the cmdlet name to see the documentation.

PSReadLine Dynamic Help
The Dynamic Help feature in PSReadLine gives a quicker way to access help in PowerShell.

Dynamic Help has changed the way I work at the console. Dynamic Help is not limited to cmdlet names. It also works to access the help information for a parameter.

The PSReadLine configuration will reset to the defaults when you close your PowerShell console. A permanent change requires an adjustment to your PowerShell profile. There is much more information for the PSReadLine module from Microsoft's documentation site and the PowerShell team blog posts.

How to use Azure more efficiently with AZ Predictor

Microsoft created the Az.Tools.Predictor module, also known as Az Predictor, to take advantage of the artificial intelligence capabilities of the Azure cloud. The Az Predictor module works in tandem with the PSReadLine module to offer code suggestions as you type.

The Az PowerShell module has more than 4,000 cmdlets to manage Azure services. It's impossible for any one person to know all the cmdlets and their parameters. Az Predictor attempts to bridge the knowledge gap by offering context-aware code suggestions. I have been pleasantly surprised by the quality, consistency and accuracy of the recommendations.

To use Az Predictor, you need at least v2.1.0 of PSReadLine installed. Next, change the PSReadLine prediction source parameter from History to HistoryAndPlugin. Plugin is shorthand for the Az.Tools.Predictor module.

Set-PSReadLineOption -PredictionSource HistoryAndPlugin

Next, install the Az.Tools.Predictor module and enable it for all sessions:

Install-Module -Name Az.Tools.Predictor
Enable-AzPredictor -AllSession

After the configuration, as you type Azure cmdlets, the Az Predictor module will offer suggestions. The formatting settings for the PSReadLine module also apply to the Az Predictor module.

In the screenshot, I typed new-az and PSReadLine displayed a list of matches for previously used commands and offered suggestions from the AZ Predictor module.

Az Predictor suggestions
The Az Predictor module complements the PSReadLine module to make predictions from both command history and the AI component in Azure.

The more you type, the fewer matches will appear as the Az Predictor module dials in suggestions. Press the down arrow on the keyboard to select a suggested entry, which provides the syntax to manage the Azure resource.

Microsoft has developed this back-end AI code for other Azure modules, so this functionality should expand to more tools.

Find easier ways to work with PowerShell output

One thing I enjoy about PowerShell is how I can customize data output. Based on what I need, I have several options, including CSV, HTML, XML, JSON or Excel spreadsheet via the ImportExcel module.

I can format my data as lists or tables, and then I can output those results in my console or to an interactive console using the Out-GridView cmdlet.

Out-GridView creates a new window. You can filter the results and change them dynamically. One advantage is you can send a long list of data to Out-GridView and then use the search filter to find specific data. Another benefit is you can select lines from the list of results to make line selections to pass back to the console for further use in PowerShell.

The following PowerShell command executes a search for some processes and pipes the results to the Out-GridView cmdlet:

get-service -name sp* -Exclude spp* | Out-GridView

Out-Gridview cmdlet
PowerShell can send output to the Out-Gridview cmdlet, which opens a form to work with the data.

ConsoleGuiTools works with data output in the same way as Out-GridView but displays results in the PowerShell console. This module was released in March 2020 as a preview release and has received regular updates and improvements. It's not production-ready, but I find it is stable for everyday use. The install process is the same as other PowerShell modules:

install-module microsoft.powershell.consoleguitools

This adds a new cmdlet Out-ConsoleGridView to work with data in the PowerShell console. The following command runs the previous search but pipes the results to the new cmdlet.

get-service -name sp*  | Out-ConsoleGridview

Out-ConsoleGridview cmdlet
Executing a search and piping the results to the Out-ConsoleGridview cmdlet results in a new view inside the PowerShell console.

In this new view, you can filter text in the gray bar. When you type, the module finds matches. Along with filtering, you can select lines, designated by checkmarks to the left of the line.

output line selection
Select lines from the search results piped to the Out-ConsoleGridview cmdlet.

After selecting lines, press Enter and PowerShell returns the selected lines to the console to use the data for other cmdlets.

The benefit to this module is continuity. The cmdlet keeps results in the same window to continue working in the PowerShell console.

This tool works with regular expressions for filtering, which provides some unique possibilities. Be aware that the color palette for this module pulls color information from whatever PowerShell host displays the output. That means PowerShell and Microsoft Terminal could have some small differences even though they both might run PowerShell v7.

Dig Deeper on IT operations and infrastructure management

SearchCloudComputing
SearchEnterpriseDesktop
SearchVirtualDesktop
Close