Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
April 16, 2024

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
If the script requires PowerShell 7, set the value to `Core`. It would also be a good idea to specify the version.
#requires -version 7.4
Unfortunately, there is no requirement statement that will say "Windows only". But there are ways around this limitation with a little code at the beginning of your script.
Get a premium subscription for full article and archive access

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.

Want to read the full issue?
GitHub Bluesky LinkedIn Mastodon https://jdhitsoluti…
Powered by Buttondown, the easiest way to start and grow your newsletter.