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.
Contents
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.
1 2 3 4 5 |
#With Parallel Measure-command { 1..254 | foreach -parallel { $ping = New-Object System.Net.Networkinformation.Ping ; $ping.send("192.168.2.$_")}} #Without Parallel Measure-command { 1..254 | foreach { $ping = New-Object System.Net.Networkinformation.Ping ; $ping.send("192.168.2.$_")}} |
The results.. WOW Cool!
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 “?”
1 2 3 4 5 6 7 8 9 |
#New If Else Syntax $a = 5 $a -gt 6 ? "Yes" : "No" No # Or put it in a variable $b = $a -gt 6 ? "Yes" : "No" $b No |
&& 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.
1 2 3 4 5 6 7 8 9 10 11 |
# && First Command failed. SO it wont run the second command. write--host "aa" && write-host "bb" # || First Command Failed. SO run the second command. write--host "aa" || write-host "bb" # && First Command Succeeded, SO run the Second command write-host "aa" && write-host "bb" # || First Command Succeeded, SO it wont run the Second command write-host "aa" || write-host "bb" |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Check if variable is $null. If it is, do this. $x = $null $x ?? "It is null, do something: " + (get-childitem c:\install\1.vhdx) #Variable is not $null anymore. Do nothing. $x = "Not null anymore" $x ?? "It is null, do something: " + (get-childitem c:\install\1.vhdx) #Check if variable is $Null and assign the following output to it. $x = $null $x ??= "It is null, do something: " + (get-childitem c:\install\1.vhdx) $x #Variable is not $null anymore. Do nothing. $x = "Not null anymore" $x ??= "It is null, do something: " + (get-childitem c:\install\1.vhdx) $x |
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.
1 2 |
$ErrorView = 'NormalView' $ErrorView = 'ConciseView' |
The new get-error view command: