How I Write PowerShell Scripts for Microsoft 365
Selecting Effective Tools to Write PowerShell for Microsoft 365
Last week, I attended a PowerShell workshop at the TEC 2024 event in Dallas. Michel de Rooij and Jaap Wesselius, two well-known MVPs, ran the show. I was there to provide color commentary, which is the polite way of saying that I asked many annoying questions.
In any case, after the workshop one of the attendees asked me what tools and setup that I use to write PowerShell code for Microsoft 365. No one has the perfect answer as to how you should write PowerShell for Microsoft 365. To start the ball rolling, here’s what I do.
Use Visual Studio Code (VSC) to Write PowerShell for Microsoft 365
I use VSC for all my PowerShell work. It took me quite a while to get my head around using VSC because I didn’t like the PowerShell ISE and used to write with Notepad. I guess using a simple text editor is a throwback to the days when I wrote DCL scripts and ALL-IN-1 scripts at Digital Equipment Corporation. Old habits do die hard.
In any case, after learning and experiencing the advantages of VSC, I wouldn’t go back, even if Notepad (the most undervalued utility program in Windows) now supports autosave. VSC is updated monthly and keeps on improving, so it’s absolutely recommended.
Use GitHub to Store the Scripts Written for Microsoft 365
VSC is connected to the Office365ItPros GitHub repository. I used to simply publish bits of PowerShell or complete scripts in articles. That’s all very well until you need to update code to enhance its value or fix bugs (fixing bugs is a form of value enhancement). At an event in Oslo in 2018, Ståle Hansen, who used to write for the Office 365 for IT Pros eBook, suggested that I create a GitHub repository to store scripts. I’m glad that I did because GitHub is a great way to store and share code.
The Office365ITPros repository is not the best-organized store for PowerShell code that you’ll find on the internet, or even just in GitHub. It’s a bit scatty, like me. But I update the code as people suggest improvements or report bugs and add any new script that I write there, like the recent script to report sharing of files stored in OneDrive for Business accounts.
VSC makes the process easy by supporting connections to GitHub repositories. Once you link VSC to GitHub, it’s easy to synchronize (commit) any change made to a script in VSC or any new code that you write to update the repository (Figure 1).
Figure 1: Commiting a change to GitHub with Visual Studio Code
At the TEC 2024 PowerShell script-off, it was noticed that some of the competitors really struggled to find sample scripts or code snippets that could help them attack a new coding problem. Keeping everything in GitHub and having a local copy of your repository on your PC means that you can stay productive even when disconnected from the network.
Speaking of snippets, sometimes I need to write a small piece of code to illustrate a point that’s not quite a full script. I do this in VSC with a file that’s not synchronized with GitHub. Instead, it’s a simple file called “Working Stuff” that’s stored on my PC and full of PowerShell snippets that I’ve played with.
Use GitHub Copilot to Improve How You Write PowerShell for Microsoft 365
I’ve often asked if the $10/month charge for a GitHub Copilot license is worth the money: it absolutely is.
GitHub Copilot isn’t perfect (my experience using GitHub Copilot is described here). It can hallucinate and create some code that just won’t work, including calling cmdlets that don’t exist. But that’s the nature of generative AI. GitHub Copilot integrates nicely with VSC and if you keep a careful eye on what Copilot produces, it can save you time, improve your code, and even write comments.
Keeping Modules Updated
I test and run everything using the latest version of PowerShell 7. The outlier here is the SharePoint Online management module, which runs on PowerShell 5. I also use Azure Automation for various tasks.
Keeping modules up to date is important. In the last week or so, there’s been updates for the Teams (to 6.6) and Exchange Online modules (to 3.6), and the Microsoft Graph PowerShell SDK updates monthly or thereabouts. Add in SharePoint Online and other modules that you might use, and it takes some effort to keep everything updated. I use two PowerShell scripts to update:
My PC (never install PowerShell modules into your OneDrive for Business accounts. I mean, why would you?)
Azure Automation (PowerShell resources in Azure automation accounts).
Naturally, the scripts are available from the Office365ITPros repository. Happy coding!
Need more advice about how to write PowerShell for Microsoft 365? Get a copy of the Automating Microsoft 365 with PowerShell eBook, available standalone or as part of the Office 365 for IT Pros eBook bundle.