Cross-Platform PowerShell Scripting Tips
I was at the PowerShell+DevOps Global Summit last week, helping to run the OnRamp program. I was demonstrating a PowerShell example in PowerShell 7 on my Windows 11 desktop. My partner, kindly reminded me that what I was showing would only work on Windows. He was right, of course. In my haste to prepare the demo, I didn't take cross-platform compatibility into account. And I should. You should. Even though you may not be specifically writing code that will run cross-platform, it is possible that code you write today may end up on non-Windows machines. Or maybe you are developing code on a Mac that might be run on Windows. I thought it would be a good idea to review items you should keep in mind when writing PowerShell code that might run cross-platform.
#Requires
I would recommend you at least take advantage of the #requires
feature of PowerShell scripts. If you are writing a script that will only work on Windows PowerShell, specify the PSEdition
.
#requires -PSEdition Desktop