Top 10 new features of PowerShell 7

PowerShell 7 is now based on .NET Framework 3.1 which is open-source. This means it is no longer necessary to use two separate versions of PowerShell for Windows and other platforms, since PowerShell 7 is simply a nice combination of Windows PowerShell and PowerShell Core. So, there will not be updated versions of Windows PowerShell or PowerShell Core anymore. There will be only one PowerShell going forward! In this post, we will look at the new features in PowerShell 7.
Latest posts by Baki Onur Okutucu (see all)

Before we take a glance at the top features of PowerShell, let's have a look at some facts about PowerShell 7.

  • exe, the executable that starts PowerShell, has now been replaced by pwsh.exe.
  • PowerShell 7 and Windows PowerShell can run side-by-side.
  • If PowerShell Core 6.x is already installed on your computer and you install PowerShell 7 on top of it, then PowerShell 7 will remove the existing PowerShell Core 6.x.
  • PowerShell 7 is LTSB (Long-Term Servicing) and its support ends in December 2022.
  • The next version of PowerShell will be based on .NET Framework 5, which will also be open-source and cross-platform.
  • The default installation path for PowerShell 7 on Windows is %programfiles%\PowerShell\7. It is /opt/microsoft/powershell/7 for Linux and /usr/local/microsoft/powershell/7 for macOS.

Now, let's dive into the cool features that come with PowerShell 7.

Here are the latest and coolest features of PowerShell 7.

Ternary operators ^

This feature simply acts like "if-else" statements. It allows us to evaluate the condition expression and take the desired action according the value it returns. So, in a single line, we can define the condition and the actions based on the result using the following pattern:

When $number = 5 and the following command is run:

$number -gt 6 ? (Write-Host "the number is greater than 5") : (Write-Host "the number is less than 5")

it returns

as the given number is equal to 5, which is not greater than 6. Since the expression after the colon (:) represents the $false condition, only

is executed.

Null Condition operators ^

Null condition operators are used to check whether the value of a variable is $null. Use cases are as follows:

$variable ?? "This command is run if the value of $variable is set as $null"

$variable ??= "This value is set as the new value of $variable if the current value of $variable is $null."

So, with the example below, when

The output will be

as the null condition is in place.

Another example:

the new value of $name will be

Chain operators ^

Chain operators are also used to check the first part of a pipeline to determine whether to execute the command in the second part of the pipeline based on the condition.

There are two chain operators that we can use. The first one is "&&", which is used between two statements in a pipeline where the second part is only executed when the first part is a valid command. Otherwise, the second part is omitted.

So,

will return the following:

&& operator in a scenario where the first command succeeds

&& operator in a scenario where the first command succeeds

as the command before the "&&" sign is a valid command.

The second example below shows how PowerShell reacts when the first command fails.

&& operator in a scenario where the first command fails

&& operator in a scenario where the first command fails

 Because the first part was not a valid command, the second part was skipped and not executed.

The second operator is "||". It is also used between two statements in a pipeline where the second part is never executed when the first part does not return any error. Here is an example:

The above command returns the following output when there is a process called "notepad".

Use of the || operator where the first command succeeds

Use of the || operator where the first command succeeds

The following output is returned if the same command is executed when there is no process called "notepad".

Use of the || operator where the first command fails

Use of the || operator where the first command fails

This time, the second part was executed as expected, as the first part failed.

Use of the Tab key to set system variable values ^

The Tab key can be used to get possible options when setting system variables. When you enter an invalid value, PowerShell normally returns an error in which it also suggests possible options that you can choose.

Assigning values to system variables

Assigning values to system variables

In PowerShell 7, it is even easier. You can simply press the Tab key when setting up such variables to list the options.

The Tab key can now be used when assigning values to system variables

The Tab key can now be used when assigning values to system variables

New version notification ^

PowerShell 7 keeps checking the version that is installed on your computer to ensure that it is up to date. If there is a newer version available (including a preview build), it prompts you when you launch PowerShell.

New version notifications in PowerShell 7

New version notifications in PowerShell 7

Foreach parallel ^

Foreach no longer needs to be used in a PowerShell workflow.

With PowerShell 7, the "Foreach-Object" cmdlet comes with a new "Parallel" parameter, which gives us the ability to run script blocks with multiple concurrent threads. The number of parallel executions that PowerShell can run at a time in a script block can be specified with the "ThrottleLimit" parameter. The default value for "ThrottleLimit" is 5, which means PowerShell can run the same script block with five different processes in parallel. Thus, it gives us improved performance in supported bulk operations.

Here is an example:

The first attempt is to measure a command to see how long it takes to finish the job. This example gets the processes one by one and waits for one second each time.

Foreach Object without the Parallel parameter

Foreach Object without the Parallel parameter

The time it takes to finish the job is around 290 seconds.

Now, let's do the same thing with a new -Parallel switch.

Foreach Object with the Parallel parameter

Foreach Object with the Parallel parameter

Almost 4 seconds or 72 times faster than a regular approach, to be exact!

Select String ^

This useful feature highlights a string when a specific string value is found in the content.

Select String highlights the specified keywords

Select String highlights the specified keywords

This is pretty useful when you need to search a keyword in content comprising thousands of lines.

Concise error views ^

Error messages in PowerShell 7 are now much simpler to understand. When $ErrorView is set to "ConciseView," PowerShell starts returning concise error messages, as the name implies.

In PowerShell 7, errors are easier to understand

In PowerShell 7, errors are easier to understand

Also, a new cmdlet, Get-Error, gives us detailed information about the error.

Get Error cmdlet gives much more detailed information about an error

Get Error cmdlet gives much more detailed information about an error

Experimental features ^

In addition to the new features, there are also some experimental features available in PowerShell.

These can be listed and enabled as shown below. Restarting PowerShell is required for the changes to take effect.

Experimental features in PowerShell 7

Experimental features in PowerShell 7

One of the nice experimental features in is "PSCommandNotFoundSuggestion", which enables PowerShell to make alternative command suggestions whenever a command that a user runs is not found.

PowerShell 7 gives you suggestions when a command not found

PowerShell 7 gives you suggestions when a command not found

Backward compatibility ^

As PowerShell 7 is now based on .NET Framework Core 3.1, it offers enhanced backward compatibility with Windows PowerShell modules. As part of this move, it now supports more modules that come with a GUI, such as Out-GridView and Show-Command. Another significant change in module compatibility is a new parameter, "UseWindowsPowerShell", which can be used with the "Import-Module" cmdlet. This way, PowerShell 7 creates an additional compatibility layer using an implicit proxy in the background toward Windows PowerShell, allowing any cmdlets in that specific module to be executed. This special session, called "WinPsCompatSession", can be seen using the Get-PSSession command.

Module compatibility in PowerShell 7

Module compatibility in PowerShell 7

Conclusion ^

PowerShell 7 as a "One PowerShell for All" approach will be the only path going forward until a new version arrives. As the next version will be based on .NET Framework 5, it wouldn't be wrong to expect many new features aligned with .NET Framework 5 in the future.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

3+
avatar
Share
2 Comments
  1. Fred Beck 2 months ago

    I feel lazy asking without looking more but, if I install PS7 on my machine, start running remote code on other machines, shouldn't they also have PS7 installed?  I guess I'm not entirely following the backward compatibility features. 

    0

  2. It should work just fine with regular PS Remoting. The the best of my knowledge, the machine you connect to will be limited to the features on that version of PS, instead of the version you are running.  (You are serializing your commands and data, sending it over to be deserialized & processed, the results are serialized, sent back and deserialized).

    There are several WinPS 5.1 cmdlets that won't run properly in 7 because of the .Net Framework requirements, however, the module provides *some* of those capabilities.  

    David F. 

    1+

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account