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:
Condition ? <The command that is run If True> : <The command that is run If False>
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")
"the number is less than 5"
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
(Write-Host "the number is less than 5")
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
$name = $null
$name ?? "the value of name is null"
The output will be
the value of name is null
as the null condition is in place.
$name = $null
$name ??= "Jack"
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.
"Hello" && "World"
will return the following:
as the command before the "&&" sign is a valid command.
The second example below shows how PowerShell reacts when 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:
Get-Process notepad -ErrorAction SilentlyContinue || "This is executed because there is no Notepad process running at the moment"
The above command returns the following output when there is a process called "notepad".
The following output is returned if the same command is executed when there is no process called "notepad".
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.
In PowerShell 7, it is even easier. You can simply press the Tab key when setting up such variables to list the options.
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.
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.
The time it takes to finish the job is around 290 seconds.
Now, let's do the same thing with a new -Parallel switch.
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.
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.
Also, a new cmdlet, Get-Error, gives us detailed information about the 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.
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.
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.
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.