© Adam Bertram 2020
A. BertramBuilding Better PowerShell Codehttps://doi.org/10.1007/978-1-4842-6388-4_2

2. Don’t Reinvent the Wheel

Adam Bertram1  
(1)
Evansville, IN, USA
 

You are surrounded by a community of coding professionals that has, for a very long time, given a lot of their work away for free. By searching back through all of the community work that has been written, you can find a treasure trove of quality code that has been used by thousands of people. Although it gives us warm fuzzies to think that our code is so unique and so special that no one has ever thought to write what we’ve written, a simple search will show how many people in the community have already accomplished (albeit with varying degrees of success) the same thing you’re trying to accomplish.

I don’t say this to try and put you down or shame you for not having ideas that are good enough; all I’m trying to do is get you to reuse the code that was written before you and to leverage others’ work. This will save you time, brainpower, and all of those creative juices that you need to write your best code possible.

Use Community Modules

The PowerShell Gallery is a great place to find community modules that are available to the public. The PowerShell Gallery is a repository of packages and modules for you to discover and download modules, read public documentation, and even update the modules from the PowerShell terminal! Once you’re comfortable, you can even contribute back to the community by uploading your own modules and scripts for public consumption.

The next time you’re looking for some PowerShell code to perform a task, do a search on the PowerShell Gallery first. A module may already be out there that takes care of the task you’re after. In your PowerShell console, use the Find-Module command and specify a name with a wildcard. For example, perhaps you need to find a module that automates something with the popular Trello service.

In your PowerShell console, run Find-Module -Name *Trello*. You’ll see a few different modules available. If you see one you’d like to try out, pipe it to Install-Module. This will immediately install it and allow you to begin exploring its commands.
Find-Module -Name PowerTrello | Install-Module

Further Learning

Leverage Others’ Work

Code reuse is becoming just as much of an art form as it is a skill. Thankfully, as more time goes on, this is becoming more of the norm than the exception. By practicing and developing your code reuse skills, you’ll be able to integrate and operate more code than you would otherwise. In turn, this will help you create code with a higher degree of complexity or solve more high-level problems than you’d be able to do if you were trying to code everything yourself.

One of the best ways to leverage others’ work is by searching GitHub. Go to the GitHub Advanced Search page and provide some criteria around the work you’re doing. Since you’ll be working with PowerShell, be sure to include searching for extensions PS1 or PSM1 to find only PowerShell scripts.

Further Learning

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230