Pipeline or Script? That is the Question
When I teach PowerShell classes, I often start by assuring students that, with the shell, you can _accomplish a great deal without ever writing a script. _And it’s true - you can. Unlike predecessor technologies like VBScript, PowerShell lets you pack a lot of goodness into a one-liner - or even into several lines run manually in the console.
What I never say is _you can accomplishÂ
anything
without ever writing a script. _That isn’t true. I see folks struggle all the time to squeeze something into a one-liner pipeline, when life would be so much easier if they switched a script-style, procedural approach.
So what’s the tipping point?
Actually, it’s really easy to spot. You should be writing a script if:
You need to take different actions based on some condition, like send an e-mail if there’s data to send, but send nothing if there’s no data.
- You need to do more than one discrete task. Yeah, you can sometimes jam multiple actions into a one-liner using things like passthrough, but it’s not consistently available, and the command becomes dreadfully difficult to read and debug.
- You need to run a command repeatedly over time, and each time some of its values will change (scripts offer declarative parameters).
Many smart folks start in the console to test a command, and then paste it into a script they’re working on (I do that, too). And there are other reasons to switch from “running a command in the console” to “banging out a script in the ISE [or editor of choice].” What tips would you offer to a PowerShell newbie to help them get the most from the command-line… but know when it’s time to move into a script-based approach?
Related Articles
PowerShell Escape Room
PowerShell Escape Room by Michiel Hamers by Michiel Hamers https://about.me/michielhamers/ Why on earth you want to create an Escape Room with PowerShell as backend? I’ve always been a fan of escape rooms, so I decided to create my own for my kids. I wanted to make it something that would be challenging and fun for them, but also educational. I decided to use PowerShell as the backend for the escape room, as I’m a PowerShell developer and I thought it would be a great way to learn more about the language.
Microsoft Graph PowerShell Module: Getting Started Guide
Microsoft Graph PowerShell Module: Getting Started Guide by Jeff Brown Microsoft is retiring the Azure AD Graph API sometime after June 30, 2023 (announcement). This retirement includes the Azure AD PowerShell module. In its place, Microsoft has released the Microsoft Graph PowerShell module. The Microsoft Graph PowerShell module is the next-generation way of managing Microsoft cloud services using PowerShell. If you have used MSOnline or Azure AD PowerShell in the past, you’ll need to read on to learn about this new module.
ICYMI: PowerShell Week of 08-October-2021
Topics include VMWare, Windows 11, Web Reports and more… Special thanks to Robin Dadswell, Prasoon Karunan V, Kiran Patnayakuni and Kevin Laux How to gather your vCenter inventory data with this VMware PowerShell script by Scott Matteson on 7th October Inventory reports are a common request when administering a VMware vCenter environment. Learn how this VMware PowerShell script can make such requests quick and easy Building a Web Report in PowerShell, use the -Force Luke by Chris Noring on 8th October