4 min read
How to Leverage IT Automation for Maximum Efficiency
Are you ready for a transformation of your IT operations? Our brand-new white paper, Maximizing IT Automation: The...
ScriptRunner Blog
Windows PowerShell 5.1 is usually preinstalled and the default – is it worth switching to PowerShell 7, or will it lead to more work and problems?
In 2018, PowerShell Core was released in version 6, bringing a breath of fresh air to the PowerShell world: platform independence! Thanks to .NET Core, PowerShell now runs not only on Windows, but also on MacOS and Linux.
The catch? Windows PowerShell 5.1 is still the standard in many organizations because it comes preinstalled. Support for PowerShell 5.1 is tied to the version of Windows you are running. This means that PowerShell 5.1 will be around for a while, but no new features will be added. As a result, many administrators are wondering whether it is even necessary to upgrade to PowerShell 7 because of the increased administrative overhead (installing and maintaining PowerShell 7 on clients and servers) or compatibility issues with existing scripts.
The latest version of PowerShell is easy to install on Windows, Linux, and MacOS. A consolation for the lack of pre-installation of the latest version of PowerShell on Windows systems: Since version 7.2 (Windows version 1709 and later), PowerShell can be updated using WSUS, which greatly reduces the administrative burden on administrators. The following are the commands to install on each operating system:
winget install --id Microsoft.Powershell --source winget
brew install --cask powershell
###################################
# Prerequisites
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages
sudo apt-get install -y wget
# Get the version of Debian
source /etc/os-release
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/debian/$VERSION_ID/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Delete the Microsoft repository GPG keys file
rm packages-microsoft-prod.deb
# Update the list of packages after we added packages.microsoft.com
sudo apt-get update
###################################
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh
Regardless of the operating system that you are using, PowerShell 7 can be started from the Terminal using "pwsh". You can still use $PSVersionTable to check the current version:
Although PowerShell 7 offers many benefits and new features, let us first take a look at some of the reasons not to upgrade. Here are the most important things to consider.
A major shortcoming of PowerShell 7 is the absence of certain cmdlets that were available in Windows PowerShell 5.1. This is especially true for cmdlets that are Windows-specific and not supported on other platforms, such as MacOS or Linux. Here are some examples:
For a complete list of removed cmdlets, see the official Microsoft documentation page: Differences between Windows PowerShell 5.1 and PowerShell 7.x.
Unlike Linux and MacOS, the same cmdlets are generally available on Windows systems.This is not because Windows ships with a different version of PowerShell 7, but because PowerShell 7 scans the system and finds the PowerShell 5.1 cmdlets. To make this easier, the UseWindowsPowerShell parameter has been added to the Import-Module cmdlet. This parameter creates a proxy module in PowerShell 7 that uses a local Windows PowerShell process to implicitly run all of the cmdlets that are included in that module.For more information, see the official documentation for Import-Module.
Another drawback is the lack of a native ISE alternative in PowerShell 7. Many administrators and developers are accustomed to Windows PowerShell 5.1's ISE, while PowerShell 7 relies on Visual Studio Code (VSCode) as its primary development environment. VSCode offers many advantages and is very powerful, but the transition can require some learning and is not always as seamless as the classic ISE. However, due to the improved features, community extensions, direct Git integration, and much higher quality developer experience, I generally recommend using Visual Studio Code. Visual Studio Code also supports Windows PowerShell 5.1 and is a cross-version tool for PowerShell coding.
Now that we've looked at the downsides of PowerShell 7, let's focus on the many upsides. There are many good reasons to upgrade to the latest version.
PowerShell 7 is faster than its predecessor in many ways. This is especially true of the Group-Object and Sort-Object cmdlets, which are now much faster. Anyone who frequently processes large amounts of data will quickly appreciate the increased efficiency.
The following sample code runs a small benchmark that illustrates the immense performance improvement in PowerShell 7. It starts by creating a large object and then grouping it using Group-Object. This grouping process is measured using the StopWatch class:
$AllFiles = Get-ChildItem -Path $home -File -Recurse -Force -ErrorAction SilentlyContinue
$stopwatch = [Diagnostics.StopWatch]::StartNew()
$suspicious = $AllFiles | Group-Object -Property Length
$stopwatch.Stop()
$duration = $stopwatch.Elapsed.TotalSeconds
Write-Output "duration: $duration seconds"
The code works with both Windows PowerShell 5.1 and PowerShell 7 – feel free to try it out. On my virtual machine, I get the following results:
The example shows that PowerShell 7 is 57 times faster. Especially in large infrastructure environments or when using large data collections, switching to PowerShell 7 can provide a massive performance boost.
One of the most powerful new features is the ability to parallelize. You can use the foreach cmdlet and the ThreadJobs module to run tasks in parallel. This means that you can perform multiple tasks at the same time, saving you a lot of time. Here is an example:
$procs = Get-Process
$procs | ForEach-Object {
Write-Host $_.Id
Start-Sleep -Seconds 1
}
In this case, all running processes are collected and their IDs are output one by one. After each output, the script waits one second. The processes are run sequentially. However, it is also possible to process multiple process IDs at the same time without much effort – in this case, the magic word is not "please", but "parallel":
$procs = Get-Process
$procs | ForEach-Object -ThrottleLimit 20 -Parallel {
Write-Host $_.Id
Start-Sleep -Seconds 1
}
This will output 20 process IDs at once, pause for one second, then output another 20 processes, and so on. The number of concurrent jobs is determined by the ThrottleLimit.
PowerShell 7 also includes many new and improved cmdlets that make your job easier. Invoke-WebRequest and Invoke-RestMethod are now more powerful and flexible, making it easier to work with Web APIs. The Test-Connection -AsJob cmdlet lets you run connection checks as background tasks, making your processes more efficient. And Test-Json lets you easily check the syntax of JSON data, which is especially useful if you work a lot with JSON files and APIs. For more information, see the official Microsoft documentation ("Test-Json").
In many organizations, people tend to prefer what is already available. This means that Windows PowerShell 5.1 is still widely used because it is preinstalled on most Windows systems. This convenience means that the transition to a new version, such as PowerShell 7, is delayed.
The ideal solution would be to integrate PowerShell 7 directly into Windows, as was done with version 5.1. This would greatly increase enterprise adoption by eliminating additional installation steps and giving IT administrators immediate access to the new features. Until this happens, the use of PowerShell 7 in many organizations will be limited to specialized applications and advanced IT departments.
Despite its many benefits, PowerShell 7 has not yet fully established itself in the broader DevOps community outside of Windows and Azure. Many users still rely on PowerShell 5.1, largely because of existing infrastructure and scripts. Therefore, a pragmatic approach is to use both versions side by side. This side-by-side approach allows you to take advantage of the new features and performance improvements in PowerShell 7 where it makes sense, while still allowing existing scripts and processes to run in the familiar environment.
So my recommendation is: Use PowerShell 7 wherever you can! Microsoft may eventually stop supporting PowerShell 5.1. Save yourself the hassle and avoid a more expensive migration later by writing scripts in version 7 now. The investment in the new version will pay off in the long run because PowerShell 7 is more future-proof and offers better performance and enhanced functionality.
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.
The poster is available for download and in paper form.
Nov 28, 2024 by Heiko Brenn
Are you ready for a transformation of your IT operations? Our brand-new white paper, Maximizing IT Automation: The...
Nov 21, 2024 by Guy Leech
Perhaps it is because I began my professional coding career writing device drivers for Unix systems in C, back in the...
Nov 20, 2024 by Damian Scoles
What is Microsoft Purview for you? Do you already master data governance with it? Learn how PowerShell simplifies...
Philip Lorenz works as a DevOps and cloud engineer. In addition to this activity, he holds training courses and creates learning content on the topics of PowerShell, automation and cloud computing.