PowerShell 7 Everything you need to know

Posted by

Microsoft released PowerShell 7 core. This version runs on the .NET Core 3.0. Cool about this new release is that they claim that the new core version almost 90% compatible is with Windows PowerShell. Another cool thing to tell is that the next major PowerShell releases will follow the .Net Core releases, with monthly fixes and pre-release features. In this blogpost I will discuss some cool new features and some handy nice to know things.

My favorite: Foreah -Parallel

The most amazing new feature (in my opinion) is the foreach parallel feature. Which makes it possible to run scripts in other runspaces easely. This was possible before PowerShell 7, but it was hard to implement correctly in scripts. Don’t go blind on a parallel foreach, check first if it should run faster. In a lot of situations it takes longer to create the new runspaces which are needed for the -parallel function than the total foreach runtime is. Because the need of loading all the variables and modules in each runspace.
To avoid blowing your CPU cores to high numbers you can use the -Throttlelimit parameter to your number of cores for example.

Of course we have tested this functionality. Pinging a whole subnet is an example of a perfect parallel usecase. To measure the scriptblock total runtime we use the famous measure-command.

The results.. WOW Cool!

When looking at the results we see that the parallel script was running 5 minutes faster.

The simplified IF Else statement

If your write much PowerShell scripts you can dream the If Else statements. If you want your script to be waterproof you use this statement a lot. With PowerShell 7 its possible to write that whole If Else statement in one simple operator, the question symbol “?”

&& and || Pipeline chain operators PowerShell

Writing fault proof scripts is a hard job. The possibility to use the new chain operators make our lives a bit easier.  The && operator executes the right-hand pipeline, if the left-hand pipeline succeeded. While the || operator executes the right-hand pipeline if the left-hand pipeline failed.

The output of the script

Check if the variable is $null and if it is $null assign a value to it

Not my favorite new feature but yeah it could be handy in some situations. With the operators ?? And ??= you can check if a variable is $null or not. And if its $null you can assign something to it.

In the examples below you see the following.
First the ?? Operator. We see that its running de commands behind the operators because the value is $null. But they are not assigned to the variable.

Second the ??= Operator. We see that the output is assigned to the $x variable because the value was $null. But it only happens when the variable is $null exactly.

The new Error view

With PowerShell 7 you can choose your error message layout and displaying your previous errors with the get-error command. The new standard default layout for error messages is the “Concise View”. But if you liked the old error messages you can change the layout to “Normal View”.

Its very easy to change the error layout. Just change the $ErrorView variable.

The new get-error view command:

Leave a Reply

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