What’s new in PowerShell 7 – Check it out!

As you know, the PowerShell team just released the new PowerShell version called PowerShell 7. PowerShell 7 will brings a couple of new features and enhancements not only for users who used PowerShell Core 6 but also for people who are currently using Windows PowerShell 5.0. I want to quickly give you an overview of what’s new in PowerShell 7.

New feature enhancements – What’s new in PowerShell 7

There are many enhancements in PowerShell 7 to make it the best PowerShell version yet. It is almost impossible to list all of them in one single blog post, so I will focus on the once, which are the most important once for me.

Out-GridView, -ShowWindow and other GUI cmdlets are back on Windows

With .NET Core 3 brining back WPF support on Windows, the PowerShell team was able to bring back some of the popular graphical tools and cmdlets like Out-Gridview, Show-Command, and the Get-Help -ShowWindow.

ForEach-Object -Parallel

Since I started using PowerShell and get into working with objects, ForEach-Object was one of the most critical cmdlets. By adding the -Parallel parameter to the ForEach-Object cmdlet, you can execute a scriptblock in parallel, similar to the option we had with PSWorkflow. With that performance can get so much better if you are working with a broad set of objects. You can also optionally set the maximum threads, which will be used in parallel (the default thread count is set to 5) with the -ThrottleLimit parameter.

Import Windows PowerShell Modules in PowerShell 7

Not all Windows PowerShell modules are currently working with .NET Core. With PowerShell Core 6, we had the Windows Compatibility Module, which allowed us to import Windows PowerShell modules that were not compatible with PowerShell Core 6 by leveraging WinRM. In PowerShell 7, the Powershell team has included to directly import a Windows PowerShell module using Import-Module and -UseWindowsPowerShell without relying on WinRM. However, you will still need Windows PowerShell 5.1 on the machine.

Clipboard Cmdlets

With Windows PowerShell, we got cmdlets to interact with the clipboard. Since we moved to a cross-platform version, we lost these cmdlets. Now they are back! You can use Get-Clipboard and Set-Clipboard on Windows, Linux, and macOS. Currently, only text is supported, and on Linux, xclip is required.

Get-Counter (Windows Performance Counter)

You can again use the Get-Counter cmdlet to get Windows performance counter information.

Out-Printer (Windows)

With the Out-Printer cmdlet, you can send a PowerShell object to the printer (Windows only). This also works with PDF printers in Windows 10.

Clear-RecycleBin (Windows)

On Windows, you can now use the Clear-RecycleBin cmdlet to empty the Windows recycle bin.

Test-Connection

The Test-Connection cmdlet got updated to give you a more consistent experience between Windows PowerShell and PowerShell 7.

Null assignment and coalescing operators (?? and ??=)

You remember my blog post about how you can check if a PowerShell variable is $null, how you basically used an if..else statement to check if the variable is $null. In PowerShell 7, we have new Null conditional operators to make this simpler.

With the new ?? null coalescing operator to get the value of a statement if it’s not $null or return something else if it is $null. And with the new ??= null conditional assignment operator, you can easily assign a variable a value, but only if the variable is $null.

Get-Hotfix (Windows)

Thanks to .NET Core 3, the Get-Hotfix cmdlet is back in PowerShell 7. Get-Hotfix gets hotfixes and updates that are installed on a local or remote computer.

Emphasis with Select-String

You might remember my blog on how you can use Select-String in PowerShell, just like grep on Linux. Now with PowerShell 7, Select-String just got improved and added emphasis to highlight the text in a string that matches the selection criteria.

New Error View with ConciseView

I am working with PowerShell for a while now, and when you get an error, all the red text and information can be overwhelming. I am happy that PowerShell 7 addresses that with the new ConciseView for errors. The $ErrorView preference variable allows you to change how errors are formatted. Before, we just had NormalView (the default view) and CategoryView.

The new ConciseView tries to return just the relevant error message, without the additional noise.

The new Get-Error cmdlet

As you can see, the new ConciseView gives you more precise but limited information on the error, which can make it easier. However, in many cases, you want to have much richer information on the error.

Therefore the PowerShell team added a new cmdlet called Get-Error, which will, by default, will provide you with a formatted view of the most recent error, including showing specific nested types like Exceptions and ErrrorRecords making it easier to troubleshoot.

Pipeline chain operators (&& and ||)

One of the new features in PowerShell 7 are the && and || operators to conditionally chain pipelines. These operators are known in PowerShell as pipeline chain operators. They work very simply. The && operator would execute the right-hand pipeline if the left-hand pipeline succeeded. The || operator would run the right-hand pipeline if the left-hand pipeline failed.

For example, if you want to create a new folder and copy files to that new folder, you can use the pipeline chain operators. The left-hand pipeline would create the folder, and the right-hand pipeline would then start copying the files, you only want to copy the files, if the folder was successfully created.

Ternary operator

You might have written many if..else statements to give back a value depending on true or false. The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions. The ternary operator is popular in the C# sharp community.

PowerShell Desired State Configuration (DSC) improvements (Experimental)

DSC Configuration Compilation – You can now compile a DSC Configuration script to a mof file on non-Windows machines. Before you needed a Windows machine to compile the DSC Configuration script.

– You can now compile a DSC Configuration script to a mof file on non-Windows machines. Before you needed a Windows machine to compile the DSC Configuration script. Invoke-DscResource – You can now leverage DSC resources while by-passing the Local Configuration Manager (LCM). You can now author your own LCM or leverage existing DSC resources within your scripts.

Long Term Servicing

PowerShell 7 GA will also be our first Long Term Servicing (LTS) release, which is a change from the current Modern Lifecycle support for PowerShell Core 6. PowerShell 7 will be supported for as long as .NET Core 3.1 is supported.

Even more:

Update-List is back

Start-Job -WorkingDirectory

Consider DBNull and NullString as $null

Read-Host -Prompt works for all input

Support negative numbers with -Split operator

pwsh as a login shell

Format-Hex improved formatting

Extended Unix Filesystem Information

A ton of other features and quality-of-life improvements too numerous to list out here

How to install and update PowerShell 7

PowerShell 7 is an additional installation to Windows, macOS, or Linux. You can download PowerShell 7 from here. If you are looking for an automated way of installing and updating PowerShell 7, check out my blog post.

Before showing you the one-liner option to install PowerShell 7, I want to share with you the documentation to install PowerShell 7 on different operating systems like Windows, macOS, and Linux.

I hope this blog gave you a great overview of what’s new in PowerShell 7. What is your favorite feature or enhancement in PowerShell 7, please let me know in the comments. By the way, if you are a Windows 10 user, you should also definitely try out the new Windows Terminal.