The next major version of PowerShell is out, and it brings significant changes. The seventh version includes features such as parallel execution, import of modules for remote execution, new operators, etc. In this article, we look at all the new features on PowerShell 7.0.
PowerShell Directory Changes
When you install PowerShell 7, it installs in a new directory and runs with PowerShell 5.1. If you are upgrading from PowerShell Core 6.x, it will replace the PowerShell 6 directory and delete any unnecessary files. Here is the list of directories you should know:
- PowerShell 7 is installed on
%programfiles%PowerShell7
- the
%programfiles%PowerShell7
the folder is added to$env:PATH
PowerShell 7 installation packages upgrade previous versions of PowerShell Core 6.x:
- PowerShell Core 6.x on Windows:
%programfiles%PowerShell6
is replaced by%programfiles%PowerShell7
- 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 to explain each of these features in a nutshell and how it will help PowerShell users. However, be sure to read more in detail on the official Microsoft page.
- Pipeline parallelization
- New operators
- ConciseView and Get-Error cmdlet
- Automatic new version notifications
- Call DSC resources directly from PowerShell 7
- Compatibility layer
Always make sure to check the environment variable before testing the actual codes.
1]Pipeline parallelization
You can now run or manage objects in parallel instead of the sequence method when using ForEach-Object -Parallel. In our article on uninstalling WIM, this method can unmount three WIM images in just under 10 seconds instead of almost 25 seconds when executed in sequence. Here is a sample code for this:
Get-WindowsImage -Mounted | foreach -Parallel {Measure-Command {Dismount-WindowsImage -Discard -Path $_.Path}}
2]New operators
There are three new operators …Ternary operator: one? b: c, pipeline chain operators: || and && and Null conditional operators: ?? and ?? =. They behave as behaves like a simplified if-else statement. They make it easier to write code instead of using the If-else loop all the time.
3] ConciseView and Get-Error cmdlet
ConciseView is a user-selectable view that is enabled as the default view in PowerShell 7. If the error is not from the script, you will receive a single line error. However, if it comes from the script or if there is a parsing error, you will receive a multiline error message and a pointer indicating on which line the error occurred.
Then you have a new cmdlet Get-Error this can help you get a detailed view of the error if you want. It can display all the details, including internal exceptions, of the last error that occurred.
$Error | Get-Error
Get-Error -Newest 3
4]Automatic new version notifications
Starting with PowerShell 7, the system will check for updates once a day and notify a new version if it is available. The information is only displayed at the start of subsequent sessions. Three indicators are available for the PowerShell update
- Default GA, Preview and RC versions
- Of disables update notification
- IT IS notifies only of updates to GA Long Term Maintenance (LTS) versions
If you want to turn off update notifications in PowerShell 7, run this command in the PowerShell window.
$Env:POWERSHELL_UPDATECHECK = 'Off'
5]Call DSC resources directly from PowerShell 7
The Invoke-DscResource cmdlet executes 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 using DSC resources. This cmdlet also allows resource debugging when the DSC engine is running with debugging enabled.
6]Compatibility layer
It allows PowerShell users to import modules into an implicit Windows PowerShell session. Using this, you will be able to execute the actual command as a session on the remote computer and return the results to the local session. Now that it supports importing modules, you can run these modules on remote computers.
PowerShell is also open source software available on Linux and macOS. It’s great to see so many new features in PowerShell 7, and we hope it continues to grow. You can read more about it on Microsoft.