How to write PowerShell scripts

Writing scripts is never foolproof. Here are some practical tips to help make sense of the jumble of letters and numbers.

Image: undrey, Getty Images/iStockphoto

Administrators are experiencing an increase in the number of devices that they are tasked with managing at all levels of the enterprise. According to Statista, the number of devices in 2018 is expected to reach 6.58 connected devices per end-user. Inversely, while the number of devices is only expected to rise, IT professionals are expected to do more with less.

Luckily for us, tools such as PowerShell (PS) exist that allow admins the flexibility to leverage the infrastructure, allowing a few to do the job of many. Managing devices in an efficient manner is the key objective of utilizing PowerShell to drive your automation scripts. Regardless of whether your PS scripts are single line cmdlets or multi-line, complex functions--one string binds them all--if the code isn't correct, it will most certainly fail.

Keeping the following points in mind will aid in minimizing any potential issues while mitigating errors as you develop, test, and revise your code.

SEE: Manage Active Directory with these 11 PowerShell scripts (Tech Pro Research)

1. Research cmdlets

There are many cmdlets available within PowerShell, some of them offer similar functionality though slightly differ depending on the intended use and desired outcome. Getting to know the cmdlets and how they work is tantamount to putting the proper cmdlets together, making the difference between generating information in the workflow or a process stopping error.

2. Double-check your syntax

PS does an admirable job of standardizing parameters and syntax across the various cmdlets. However, individual cmdlets may contain additional, unique parameters. Become familiar with these syntax changes that may exist between cmdlets and different versions of the Windows Management Framework (WMF).

3. Annotate your code

Adding comments to your code is extremely useful for those working in teams to share useful information, such as explaining what a particular line or function does, or simply revisiting one's own code after a cooling off period to refresh your memory. Either way, including notes as part of the code can and does help the process along for both the initial writer and anyone following up afterward.

4. Compartmentalize

When working with large or complicated logical structures of code--especially those that perform many functions--don't just rely on the previous set of instructions to execute correctly before the next set can be performed. It is often simpler to break down the coding into smaller sections. This makes the writing, testing, troubleshooting, and updating processes much easier to manage.

SEE: Windows 10 spotlight: Prepare, repair, and recover (Tech Pro Research)

5. Test, test, test

It should go without saying that just about everything in the IT world needs to be rigorously tested before it is deployed. With this in mind, testing your code is imperative to ensure that it functions exactly as intended. Good thing PowerShell includes the Integrated Scripting Environment (ISE), a host application to write, test, and debug your PS scripts with built-in cmdlet and syntax information, a console with which to test cmdlets, and selective code execution allowing one to deploy specific portions of the code for verification purposes.

6. Network with mentors

We can all use a little (or a lot of) help from time to time, developers often employ coding methodologies that stress team development and peer auditing of the codebase, often performed by senior, more seasoned developers. These could be colleagues within your organization, members on a forum, or even users such as yourself that are part of a network of admins--like what you'd find on GitHub, for example--to aid in code corrections and point you in the right direction if you get stuck, even helping by reviewing scripts for errors.

7. Simplify whenever possible

Unlike the concept that bigger is better, remember that scripted code runs on computers and utilizes their finite resources to perform a task. So, executing a command contained within three cmdlets is less efficient (i.e. uses more resources) than the same process occurring from running one cmdlet (i.e. uses less resources). Furthermore, as Microsoft updates the WMF, cmdlets may be modified or deprecated, wholly changing how a function is processed. Part of simplifying your code will also require updates over time.

Also see