How to build the appropriate PowerShell parameters

windows-it-pro pwershell parametersA beginner new to writing PowerShell scripts might simply open up his favorite PowerShell script editor and just start coding out what he needs to do.  He may have specific requirements such as create Active Directory (AD) user accounts, setting permission on an Exchange mailbox or perhaps moving some files around. Whatever the case, he has specific requirements: create AD user accounts from the C:\Users.csv file, give each user full access to the shared mailbox ConferenceRoom, move all files from \\SERVER1\c$\SomePath to \\SERVER2\d$\SomeOtherPath.

These requirements are fictional but realistic. Someone doesn’t typically sit down and write a PowerShell script for the fun of it. They typically have a specific purpose to do so. They have a problem that can be solved with automation and they have chosen PowerShell for the solution. Let’s see what would happen if I write this fictional script with some pseudocode and call it DoSomething.ps1:

I run it like this: & .\DoSomething.ps1 and it works! Granted, this code isn’t actually going to work but let’s just say it did.  Do you see anything wrong here?  It solves the problem and does everything we want it to, right? Yes. But, this is not the best way to write a PowerShell script. Can you spot the reason why? No, it’s not because I didn’t use the pipeline or didn’t use the right kind of loop. It was because this script is static and has no parameters.

Every script has variables and I’m not talking about actual PowerShell variables. Every piece of code regardless of the language has “business logic” variables. These are the variables that may change over time. Today you might be importing a CSV file from C:\Users.csv but what about tomorrow or the next day? Do you really want to ensure that a CSV is located in C:\Users.csv every time you want to run this script? Is that ConferenceRoom mailbox always going to be the same name? Probably not. These are your “business logic” variables and should be used as parameters to your… Continue reading on Windows IT Pro.

Adam Bertram

Chief Automator at Adam the Automator, LLC
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He authors IT pro course content for Pluralsight, is a regular contributor to numerous print and online publications and presents at various user groups and conferences.You can find Adam here on the blog or on Twitter at @adbertram.

Latest posts by Adam Bertram (see all)