Leanpub Header

Skip to main content

Building PowerShell Modules

An in-depth guide from design to production

An in-depth guide to creating high-quality PowerShell modules including docs, tests, CI/CD, and community engagement.

This book provides a detailed guide to what PowerShell modules are, and how to design, develop, test, and publish them. It also includes numerous "good practices" for creating engaging open-source modules for the community to enjoy.

Minimum price

$19.99

$27.99

You pay

$27.99

Author earns

$22.39
$

...Or Buy With Credits!

You can get credits monthly with a Reader Membership
PDF
EPUB
WEB
331
Readers
305
Pages
67,197Words
About

About

About the Book

Overview

Are you a PowerShell scripter who wishes to take their skills to the next level? Have you ever wanted to create an open-source PowerShell module but didn't' know where to start? Then this book is for you!

Whether you are creating internal modules for your organization to use or open-source modules for the community to enjoy, you'll find something valuable in this book. This is not a "how to learn PowerShell" book. It is expected you already have the basics of PowerShell down, so things like PowerShell semantics and language features will not be the topic of this book. There are already some excellent books out there that cover the PowerShell language itself. I'd encourage you to take a look at those books first if you're interested in learning more about PowerShell as a language. Consider this book a follow-up to them where you will learn how to create useful, high-quality, and engaging PowerShell modules with real-world guidance, examples, and concept discussion.

This is an Agile-published book

Updates will be published as they are written. As PowerShell module practices evolve, so to will this book. Your one-time purchase grants you lifetime access to updates and any new content. You can even get notified via Leanpub (if you opt-in) when there's new content available, so you're always up to date.

Book Pricing

As significant parts of the book are published, the price will increase accordingly. If you've already purchased a copy, then you're all set! You won't have to pay another dime, and you'll receive any updates for free. This is an excellent reason to purchase it now before it's finished to lock in the lower price.

Book Parts

The book will include four separate parts to cover high-level concepts. Each part will consist of dedicated topic chapters relevant to that concept.

  • Part 1 - Modules Primer
  • Part 2 - Project and Module Design
  • Part 3 - The Build and Test Loop
  • Part 4 - Creating a Quality Community Project

Feedback

I love feedback. Use the "Email the Author(s)" link below to contact me. If something is unclear or you feel it is flat out wrong, let me know. All constructive feedback is welcome and ultimately creates a better book for everyone.

Share this book

Installments completed

13 / 25

Author

About the Author

Brandon Olin

Brandon is a Site Reliability Engineer, Cloud Architect, veteran Systems Engineer, speaker, blogger, freelance writer, and open source contributor. He is a Microsoft MVP in Cloud and Datacenter Management and has a penchant for PowerShell and DevOps processes. He spends much of his time exploring new technologies to drive the business forward and loves to apply ideas pioneered in the DevOps community to solving real-world business problems. Brandon is active in the PowerShell community and loves to give back with a number of projects published to the PowerShell Gallery.

You can follow his code at GitHub, his blog at devblackops.io, or reach him on Twitter at @devblackops.

Contents

Table of Contents

About the Author

About This Book

  1. PowerShell version
  2. Roadmap
  3. Part 1
  4. Part 2
  5. Part 3
  6. Part 4
  7. Bonus

Feedback

Typographic Conventions

  1. Asides

Dedication

Introduction

  1. IPart 1 - Modules Primer

1Module Basics

  1. You will learn
  2. 1.1What is a module
  3. You are already using modules
  4. 1.2Why build modules
  5. 1.2.1Organizing related functionality
  6. 1.2.2Code sharing
  7. 1.2.3Code reuse
  8. 1.2.4Versioning
  9. 1.3Terminology
  10. 1.4Module types
  11. 1.4.1Script modules
  12. 1.4.2Binary modules
  13. 1.4.3Manifest
  14. 1.4.4What is a manifest
  15. 1.5Summary
  16. Key Points

2Working with Modules

  1. You will learn
  2. 2.1Module locations
  3. 2.1.1$env:PSModulePath
  4. Cross-platform code
  5. 2.2PowerShellGet
  6. 2.2.1Finding modules online
  7. 2.2.2Installing a module
  8. 2.2.3Updating modules
  9. 2.2.4Uninstalling modules
  10. 2.3Discovering module information
  11. 2.3.1Listing installed modules
  12. 2.3.2Getting module commands
  13. 2.3.3Exploring a module
  14. 2.4Installing modules
  15. 2.4.1Creating a hello world module
  16. 2.4.2Manual installation
  17. Module locations
  18. 2.4.3PowerShellGet
  19. Default installation scope with PowerShellGet
  20. 2.5Saving modules
  21. 2.6Importing modules
  22. 2.6.1Modules are single-instance
  23. 2.6.2Module prefixes
  24. 2.7Being specific with module specifications
  25. 2.8Module auto-loading
  26. 2.8.1Controlling auto-loading behavior
  27. 2.9Removing modules
  28. 2.9.1Removing a module by name
  29. 2.9.2Removing a module by using a module specification
  30. 2.9.3Removing a module by PSModuleInfo
  31. 2.9.4Caveats
  32. 2.10Summary
  33. Key points

3Authoring a Module

  1. You will learn
  2. 3.1Basic structure of a module
  3. 3.1.1Rules and conventions for module files
  4. Always include a manifest
  5. 3.1.2Exceptions to the rules
  6. 3.2Creating a script module
  7. 3.2.1Creating a basic script
  8. 3.2.2Turning a script into a module
  9. Default script module versioning
  10. 3.3Manifests
  11. 3.3.1Manifest structure
  12. Restricted language support in manifests
  13. 3.3.2Creating a module manifest
  14. An evolving manifest
  15. 3.3.3Elements of a manifest
  16. Make sure your module IDs are unique
  17. Manifest “gotchas”
  18. 3.3.4Updating a manifest
  19. 3.3.5Testing a manifest
  20. 3.4Summary
  21. Key points

4Dealing with Module Dependencies

  1. You will learn
  2. 4.1Module dependencies
  3. 4.1.1Implicit dependencies
  4. Beware of implicit dependencies
  5. Built-in modules can differ across platforms
  6. 4.1.2Explicit dependencies
  7. 4.1.3Focusing our efforts
  8. 4.1.4Defining dependencies to other modules
  9. Options for RequiredModules
  10. 4.1.5Installing dependencies
  11. Taking advantage of what is already available
  12. 4.2Summary
  13. Key points

5Distributing Modules

  1. You will learn
  2. 5.1PowerShell repositories
  3. 5.1.1The default repository
  4. 5.1.2Trusting repositories
  5. 5.1.3Repository types
  6. 5.2Creating a local repository
  7. 5.2.1Registering a repository
  8. 5.3Publishing a module locally
  9. 5.3.1Publishing a module by name
  10. 5.3.2Publishing a module by file path
  11. When publishing modules, be specific
  12. 5.4The PowerShell Gallery, a tour
  13. 5.4.1Navigating the Gallery
  14. 5.4.2Registering on the Gallery
  15. Enable 2FA
  16. 5.4.3Managing API keys
  17. 5.5Summary
  18. Key points
  19. IIPart 2 - Project and Module Design

6Choosing a Module Layout

  1. 6.1Monolithic PSM1
  2. 6.1.1Module structure
  3. 6.2Category submodules
  4. 6.2.1Grouping by domain
  5. 6.3Dot-sourced functions from PSM1
  6. 6.3.1What happens during import
  7. 6.3.2Advantages and disadvantages
  8. 6.4Final verdict
  9. 6.5Summary
  10. Key points

7Keeping Some Module Contents Private

  1. You will learn
  2. 7.1Determining the public functions
  3. Approved Powershell verbs for CRUD operations
  4. 7.1.1The role of private functions
  5. 7.1.2Candidates for private functions
  6. 7.1.3Controlling the visibility of variables and aliases
  7. 7.2Summary
  8. Key points

8Using Classes in a Module

  1. You will learn
  2. Class bonus chapter
  3. 8.1Importing classes
  4. Having import trouble?
  5. 8.1.1The using statement
  6. Confusion with $using:
  7. Module resolution with using module
  8. 8.1.2Differences from Import-Module
  9. 8.1.3Differences from #requires
  10. 8.2Using classes internally in a module
  11. 8.2.1Trying to dot-source classes
  12. 8.3Ordering classes
  13. 8.4Extending classes from other modules
  14. 8.4.1Having a module reference classes from another module
  15. RequiredModules looks for modules in $env:PSModulePath
  16. 8.5The user experience of classes
  17. 8.5.1Providing functions to create class instances
  18. 8.6Summary
  19. Key points

9Building a Module From Many Files

  1. You will learn
  2. 9.1Benefits of a monolithic PSM1
  3. Code signing
  4. 9.2Benefits of dot-sourced functions
  5. Simple is good
  6. 9.3Creating a dot-sourced module
  7. Creating flexible modules
  8. 9.4Building a module - E pluribus enum (out of many, one)
  9. Are we really “compiling” a module?
  10. 9.4.1Creating a project directory
  11. Builds scripts are the entry point for projects
  12. 9.4.2Creating a build script
  13. PowerShell-based build frameworks
  14. 9.4.3Running the build script
  15. 9.4.4Exporting functions in the manifest
  16. 9.5Summary
  17. Key points

10Versioning a Module

  1. You will learn
  2. 10.1What is a software version
  3. Sometimes versions are just marketing
  4. 10.1.1Examples of versioning schemes
  5. 10.2Semantic Versioning
  6. SemVer v1.0.0
  7. 10.2.1SemVer rules
  8. Version 0 doesn’t replace proper planning
  9. 10.2.2Why SemVer is useful
  10. Not everyone follows SemVer
  11. 10.3Automating module versioning
  12. 10.3.1Avoid auto-incrementing
  13. 10.3.2Incrementing a module version
  14. 10.4Pre-release versioning
  15. Pre-release is for v1.0.0 and above
  16. 10.4.1Adding a pre-release moniker
  17. 10.4.2Publishing a pre-release module
  18. 10.4.3Finding and installing a pre-release module
  19. Updating a pre-release version replaces the existing version
  20. 10.5Tips for avoiding breaking changes
  21. PowerShell is consistent, except when it isn’t
  22. 10.6Handling deprecation
  23. Don’t pull the rug out from under your users
  24. 10.6.1Communication avenues
  25. 10.7Specifying compatible operating systems and PowerShell editions
  26. Finding the edition of PowerShell
  27. 10.7.1The CompatiblePSEditions module manifest field
  28. 10.8Summary
  29. Key points

11Building Better Functions

  1. You will learn
  2. 11.1Plan your dive, dive your plan
  3. 11.1.1Planning your functions
  4. 11.1.2Think about the experience
  5. 11.2Functions should do one thing
  6. 11.3Creating testable functions
  7. 11.4Self-contained functions
  8. 11.5Writing defensive functions
  9. 11.5.1Enforce mandatory parameters
  10. 11.5.2Explicitly define parameter types
  11. Beware of strings
  12. 11.5.3Use validation attributes
  13. 11.6Document your functions
  14. 11.7Adhere to approved verbs
  15. Avoid the Invoke verb
  16. 11.8Use proper parameter names
  17. 11.9Use advanced functions
  18. Don’t replicate advanced function features
  19. Alternate way to create advanced functions
  20. 11.10Support the pipeline
  21. 11.11Create safe functions
  22. 11.11.1Adding -WhatIf and -Confirm to your functions
  23. ShouldContinue breaks non-interactive scripts
  24. 11.11.2Using the -Force
  25. 11.12Sensible error handling
  26. 11.12.1Creating terminating errors
  27. 11.12.2Creating non-terminating errors
  28. 11.13Write with cross-platform in mind
  29. 11.13.1Working with the PATH environment variable
  30. Supporting older PowerShell versions
  31. 11.13.2File paths and delimiters
  32. 11.14Summary
  33. Key points

12Creating a Quality GitHub Project

  1. You will learn
  2. 12.1The README
  3. What happens when there is no README?
  4. 12.1.1Elements of the README
  5. 12.1.2Jazzing up the README with badges
  6. 12.2Choosing a license
  7. 12.3Keeping track of what changed
  8. 12.4The Code of Conduct
  9. 12.5Tell folks how to contribute
  10. 12.6Issue and pull request templates
  11. 12.6.1Template front matter and Markdown
  12. 12.6.2Pull request template
  13. 12.7Divvying up the work with CODEOWNERS
  14. 12.7.1CODEOWNERS Syntax
  15. 12.8Project sponsors and funding
  16. 12.9Summary
  17. Key points

13Documenting a Project

  1. IIIPart 3 - The Build and Test Loop
  2. IVPart 4 - Creating a Quality Community Project
  3. VBonus Chapters

14PowerShell Classes Explained

  1. You will learn
  2. 14.1What is a PowerShell Class
  3. 14.2Why use classes
  4. 14.3Defining and creating a class
  5. Creating a class using New-Object
  6. 14.4Adding class properties
  7. Type constrained properties
  8. 14.5Defining class methods
  9. Returning data from methods
  10. 14.6Initializing class properties
  11. 14.7Validating properties by using attributes
  12. 14.8Accessing the class members with $this
  13. 14.9Passing input to methods
  14. Parameters in class methods are mandatory
  15. 14.10Method overloading and signatures
  16. Passing method parameters
  17. 14.11Making properties and methods static
  18. 14.11.1Accessing static properties
  19. Common static properties
  20. 14.11.2Calling static methods
  21. Static methods only access static members
  22. 14.12Initializing a class instance with constructors
  23. Defining constructors removes the default constructor
  24. 14.13Inheritance
  25. 14.13.1Creating a derived class
  26. 14.13.2Overriding methods
  27. 14.13.3Calling base methods
  28. 14.13.4Calling constructors
  29. 14.14Hiding properties and methods
  30. Hidden class members are not protected
  31. 14.15Enumerations
  32. 14.15.1Using an enumeration
  33. 14.15.2Using enumerations in parameters
  34. Enumerations are everywhere
  35. 14.15.3Creating an enumeration
  36. 14.15.4Flag enumerations
  37. Flag values
  38. 14.16Summary
  39. Key points

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub