The next major version of PowerShell is out, and it brings in some significant changes. The Seventh version includes features like parallel execution, importing modules for remote execution, new operators, and more. In this post, we are looking at all the New features on PowerShell 7.0.
PowerShell Directory Changes
When you install PowerShell 7, it will install to a new directory, and run along with PowerShell 5.1. If you are upgrading from PowerShell Core 6.x, then it will overwrite the PowerShell 6 directory, and remove all unnecessary files. Here is the list of directories you should know:
PowerShell 7 is installed to-
%programfiles%\PowerShell\7
The %programfiles%\PowerShell\7 folder is added to $env:PATH
The PowerShell 7 installer packages upgrade previous versions of PowerShell Core 6.x:
PowerShell Core 6.x on Windows:
%programfiles%\PowerShell\6
is replaced by
%programfiles%\PowerShell\7
Linux:
/opt/microsoft/powershell/6
is replaced by
/opt/microsoft/powershell/7
macOS:
/usr/local/microsoft/powershell/6
is replaced by
/usr/local/microsoft/powershell/7
New features in PowerShell 7
I have tried explaining each of these features in brief and how it will help PowerShell users. However, make sure to read in more detail at the Microsoft official page.
- Pipeline parallelization
- New operators
- ConciseView and Get-Error cmdlet
- Automatic new version notifications
- Invoke DSC resources directly from PowerShell 7
- Compatibility layer
Always make sure to check on Environment Variable before testing out actual codes.
1] Pipeline parallelization
You can now execute or handle objects in parallel instead of the sequence method when using ForEach-Object -Parallel. In our post on uninstalling WIM, this method can dismount three WIM images in just under 10 seconds instead of almost 25 seconds when running in sequence. Here is a sample code for it:
Get-WindowsImage -Mounted | foreach -Parallel {Measure-Command {Dismount-WindowsImage -Discard -Path $_.Path}}
2] New operators
There are three new operators—Ternary operator: a ? b : c, Pipeline chain operators: || and && and Null conditional operators: ?? and ??=. These behave like behaves like a simplified if-else statement. They make it easy to write the code instead of using the If-else loop all the time.
3] ConciseView and Get-Error cmdlet
ConciseView is a user-selectable view which is enabled as the default view of PowerShell 7. If the error is not from the script, you will receive a single line error. However, if it’s from the script or there is a parsing error, you will receive a multiline error message, and a pointer showing on which line the error occurred.
Then you have a new cmdlet Get-Error that can help you to get a detailed view of the error when desired. It can display full details, including inner exceptions, of the last error that occurred.
$Error | Get-Error
Get-Error -Newest 3 # Displays the last three errors that occurred in the session
4] Automatic new version notifications
Starting with PowerShell 7, the system will check for updates once a day and will notify about a new version if available. The information is shown only at the start of subsequent sessions. There are three flags available for PowerShell Update
- Default GA, Preview and RC releases
- Off turns off the update notification feature
- LTS only notifies of updates to long-term-servicing (LTS) GA releases
If you want to turn off update notifications in PowerShell 7, execute this command in PowerShell window.
$Env:POWERSHELL_UPDATECHECK = 'Off'
5] Invoke DSC resources directly from PowerShell 7
The Invoke-DscResource cmdlet runs a method of a specified PowerShell Desired State Configuration (DSC) resource. It is an experimental feature.
Using this cmdlet, configuration management products can manage Windows or Linux by using DSC resources. This cmdlet also enables debugging of resources when the DSC engine is running with debugging enabled.
6] Compatibility layer
It allows PowerShell users to import modules in an implicit Windows PowerShell session. Using this, you will be able to the real command as a session on the remote computer and returns the results to the local session. Now that it supports importing modules, you can run these modules on remote computers.
PowerShell is open-source software available on Linux and macOS as well. It is great to see so many new features in PowerShell 7, and we hope it keeps growing. You can read more about it on Microsoft.