What’s Coming in PowerShell 7.1

0
302

PowerShell 7.1 may not look like it has huge changes at first glance, but there is a lot more going on under the hood than one may expect. There are also a number of very useful features and tools that can enhance your scripting and help you develop even better scripts. PowerShell 7.1 was released on 11/11/2020 and is quickly becoming adopted by many in the IT community. This article goes into a number of improvements and enhancements but is by no means a comprehensive collection of all the improvements.

Architectural Changes

By the biggest improvement is that of PowerShell 7.1 being built on the just-released .NET 5. This is a major release that continues the unification journey that .NET has been on with traditional .NET and .NET Core. With .NET 5 the first steps into a unified core are realized. PowerShell 7.1 takes advantage of the many performance improvements and language improvements.

Updated Core Modules and Loading Improvements

PowerShell 7.1 includes several core module updates. Notably, PSReadLine 2.1.0 now includes predictive IntelliSense, which is a fish shell-like syntax prediction engine. Additionally, there have been many fixes to the vi-mode and the added ability to set the MaximumHistoryCount from a user profile.

To improve WinCompat module loading, PowerShell 7 modules are now treated with a higher priority. This prevents inadvertent overwriting of the core modules when using the WinCompat mode to enable non-native PowerShell 7 modules to work within the updated PowerShell version.

New Engine Features

There are, of course, several new core features that are useful. The main three are:

  • PSNullConditionalOperators
  • PSUnixStat
  • TLS 1.3 support

Null conditional operators were actually introduced back in PowerShell 7 but as an experimental feature. This feature has now been made mainstream and included by default in PowerShell 7.1. There are two new operators that are ?? and ??=.

# This typically replaces an if null statement and will run the output if the value is null.
$Object ?? ‘$object contains a $null value’

# When the value of the conditional is null then, assign that object to the value on the right of the conditional.
$Object ??= ‘This value is assigned on a $null $object’

Next, we have the PSUnixStat option, this file states that it is now moved out of experimental but sometimes may still appear as experimental. This feature will only be available on Linux systems. You can find out its status and turn on the feature by doing the following.

# Make sure this feature is enabled, will need to restart the PowerShell session
Get-ExperimentalFeature -Name ‘PSUnixFileStat’ | Enable-ExperimentalFeature

This feature will give you a new UnixMode property that displays information from the Unix stat command. This is very useful for those used to working with traditional Linux permissions.

Finally, TLS 1.3 support is now included in the various web cmdlets such as Invoke-RestMethod and Invoke-WebRequest. For those that want to utilize the performance and security enhancements that are included in this new TLS version and can interact with an endpoint capable of that, this will be a very useful addition. If the site supports TLS 1.3, the content will be returned and negotiated by the new protocol version.

Invoke-RestMethod -Uri ‘<https://mysite.com>’ -SslProtocol Tls13

Additional Improvements

There are a handful of additional features and improvements that help to enhance your script abilities and structure.

  • Within the Windows environment, Start-Process will now, by default, include all environment variables from the current session. Only when using the UseNewEnvironment parameter will an environment be created with new process variables.
  • The ForEach-Object -Parallel command will now reuse runspaces. This will improve memory and performance usage. For those concerned that maximum runspace isolation is not being achieved, the parameter UseNewRunspace is now included to create a brand new runspace for each pipeline iteration.
  • The Get-Random command now includes the Shuffle parameter. This will take a collection piped into Get-Random or via InputObject and return the same collection but shuffled in order.
  • Add the parameter AsUTC to the Get-Date command. This simplifies the traditional conversion utilities needed, such as with the [DateTime] object.
  • Implement the Stop-Computer cmdlet for Linux and macOS systems. This relies on the binary /sbin/shutdown being available. If the OS is Linux, the argument passed is now and if the OS is macOS then the argument passed is -P now.
  • Similar to Stop-Computer, Restart-Computer is now implemented for Linux and macOS systems. This uses the binary /sbin/shutdown and passes the argument of -r now.

Breaking Changes

Finally, there is a handful of breaking changes. There are more listed, but these two may affect scripts more than others.

  • The rename of FromUnixTime to UnixTimeSeconds on the Get-Date cmdlet to allow Unix time input may impact scripts that use this feature and will need to be updated.
  • Do not wrap return result to PSObject when converting ScriptBlock to delegate types. This is a edge case situation when casting a scriptblock and you want the return type to be the original object and not cast to a PSObject.

Conclusion

PowerShell 7.1 may not seem like it has introduced huge new features, but there are many incremental improvements and major backend engine enhancements. The PowerShell ecosystem and language continues to improve with every iteration and is quickly becoming a popular and staple language for system administrators everywhere.