After 3 successful release candidates, the PowerShell team has released their first LTS version of PowerShell 7. With this brings many new enhancements that both bring new capabilities as well as further closing the gap between Windows PowerShell and PowerShell Core.
To check out the official release notes feel free to hop on over to the PowerShell team’s blog: https://devblogs.microsoft.com/powershell/announcing-powershell-7-0/
Wait, what about PowerShell Core?
As you may know when PowerShell Core (6) released, since it is based on .NET Core instead of .NET Framework the team made the decision to label it as “Core”. That hasn’t really changed, but going forward the team has decided to refer to it as simply “PowerShell 7”. This is because the team feels that they have successfully bridged the gap between Windows PowerShell and PowerShell Core.
OK, so what’s new?
With this release there are some new tools to help improve your experience with PowerShell 7. These will take some time to understand and get comfortable with if you aren’t familiar with their concepts from other programming languages. It is definitely something you should set aside a bit of time to read up on and better understand with practice. A comprehensive list of new features can be found in the official docs from Microsoft, but just to touch on a few of them briefly:
- Parallel looping –
ForEach-Object
gets a-parallel
switch to automatically spin up runspaces under the parent process for improved loop performance. There is also a-ThrottleLimit
parameter to control how many runspaces get spun up at a time. - Ternary operators – Something that will be familiar for those who started their journey with C#, these are essentially an shorthand alternative to writing if/else statements.
- For example:
a ? b : c
. General syntax is as follows:(conditional statement) ? (what to do if true) : (what to do if false)
- For example:
- Pipeline chain operators –
||
and&&
These are interesting, essentially the way they work is that you place these operators between code and they will handle running code based on success or failure of the left-hand code.- For example if you only wanted certain code to run if the prior operation fails:
Write-Error 'Bad' && Write-Output 'Second'
- For example if you only wanted certain code to run if the prior operation fails:
- Null coalescing operators –
??
,??=
,?.
(expirimental), and?[]
(expirimental). Directly from Microsoft:- “The null-coalescing operator ?? returns the value of its left-hand operand if it isn’t null. Otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn’t evaluate its right-hand operand if the left-hand operand evaluates to non-null.“*.
- “The null conditional assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn’t evaluate its right-hand operand if the left-hand operand evaluates to non-null.“
- Improved Error Investigations – This is something I have heard small gripes about in the DevOps community.
- The new cmdlet (Get-Error) provides an easy way to access error messages with a high level of detail.
- There is also now an, enabled by default, ConciseView that provides improved readability and color-coordination when errors do occur.
- This is able to be modified by way of setting $ErrorView to either “ConciseView” or “NormalView” if you decide you like the older view better.
- Improved backwards-compatibility with Windows PowerShell – A significant improvement in the amount of backwards compatibility with Windows PowerShell. Our beloved
Out-GridView
is back for Windows, though Tyler Leonhardt is working on an in-console alternative!Import-Module
gets a-UseWindowsPowerShell
switch parameter which will automatically create proxy sessions to Windows PowerShell.- A full list of module compatibility is being maintained here: https://docs.microsoft.com/en-us/powershell/scripting/whats-new/module-compatibility?view=powershell-7
- Automatic new version notifications – Something probably long overdue, but a nice touch to let folks know if there is a new version available.
All my scripts run on Windows PowerShell, should I switch?
The great thing is that as noted above, this release helps to bridge the gap between Windows PowerShell and PowerShell Core with integrated backwards compatibility. That doesn’t mean you should just run your scripts on PowerShell 7 and expect them to work without any maintenance. Luckily, you don’t have to really choose between the 2 while you get your scripts updated. As when PowerShell 6 was released, a new process name (pwsh.exe) was created. Therefore, you can have both Windows PowerShell and PowerShell 7 installed side-by-side without any worries.
It may be a good time to invest in some Pester framework unit tests being created to help ensure quality when making the switch from Windows PowerShell to PowerShell 7.
I’m sold, where do I get it?
If you feel like you’re ready to install, head on over to the github release page and select the appropriate version for your OS: https://github.com/PowerShell/PowerShell/releases/tag/v7.0.0
Conclusion
In conclusion, PowerShell 7 is finally out and I couldn’t be more excited to dive in. I also highly suggest that you take this as an opportunity to dive into using Visual Studio Code full time (you already should be!) as they released a new version of the PowerShell extension that significantly improves editor performance and the overall experience.