Building PowerShell Modules
Building PowerShell Modules
An in-depth guide from design to production
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.
Reader Testimonials
Jim Birley
It truly is a book that fills a long-standing gap in topics from other PowerShell books. I have been asking other people for good material on module development for quite some time. This book is it.
Table of Contents
-
- About the Author
-
About This Book
- PowerShell version
-
Roadmap
- Part 1
- Part 2
- Part 3
- Part 4
- Bonus
- Feedback
-
Typographic Conventions
- Asides
- Dedication
- Introduction
-
Part 1 - Modules Primer
-
1 Module Basics
- 1.1 What is a module
-
1.2 Why build modules
- 1.2.1 Organizing related functionality
- 1.2.2 Code sharing
- 1.2.3 Code reuse
- 1.2.4 Versioning
- 1.3 Terminology
-
1.4 Module types
- 1.4.1 Script modules
- 1.4.2 Binary modules
- 1.4.3 Manifest
- 1.4.4 What is a manifest
- 1.5 Summary
-
2 Working with Modules
-
2.1 Module locations
- 2.1.1 $env:PSModulePath
-
2.2 PowerShellGet
- 2.2.1 Finding modules online
- 2.2.2 Installing a module
- 2.2.3 Updating modules
- 2.2.4 Uninstalling modules
-
2.3 Discovering module information
- 2.3.1 Listing installed modules
- 2.3.2 Getting module commands
- 2.3.3 Exploring a module
-
2.4 Installing modules
- 2.4.1 Creating a hello world module
- 2.4.2 Manual installation
- 2.4.3 PowerShellGet
- 2.5 Saving modules
-
2.6 Importing modules
- 2.6.1 Modules are single-instance
- 2.6.2 Module prefixes
- 2.7 Being specific with module specifications
-
2.8 Module auto-loading
- 2.8.1 Controlling auto-loading behavior
-
2.9 Removing modules
- 2.9.1 Removing a module by name
- 2.9.2 Removing a module by using a module specification
- 2.9.3 Removing a module by PSModuleInfo
- 2.9.4 Caveats
- 2.10 Summary
-
2.1 Module locations
-
3 Authoring a Module
-
3.1 Basic structure of a module
- 3.1.1 Rules and conventions for module files
- 3.1.2 Exceptions to the rules
-
3.2 Creating a script module
- 3.2.1 Creating a basic script
- 3.2.2 Turning a script into a module
-
3.3 Manifests
- 3.3.1 Manifest structure
- 3.3.2 Creating a module manifest
- 3.3.3 Elements of a manifest
- 3.3.4 Updating a manifest
- 3.3.5 Testing a manifest
- 3.4 Summary
-
3.1 Basic structure of a module
-
4 Dealing with Module Dependencies
-
4.1 Module dependencies
- 4.1.1 Implicit dependencies
- 4.1.2 Explicit dependencies
- 4.1.3 Focusing our efforts
- 4.1.4 Defining dependencies to other modules
- 4.1.5 Installing dependencies
- 4.2 Summary
-
4.1 Module dependencies
-
5 Distributing Modules
-
5.1 PowerShell repositories
- 5.1.1 The default repository
- 5.1.2 Trusting repositories
- 5.1.3 Repository types
-
5.2 Creating a local repository
- 5.2.1 Registering a repository
-
5.3 Publishing a module locally
- 5.3.1 Publishing a module by name
- 5.3.2 Publishing a module by file path
-
5.4 The PowerShell Gallery, a tour
- 5.4.1 Navigating the Gallery
- 5.4.2 Registering on the Gallery
- 5.4.3 Managing API keys
- 5.5 Summary
-
5.1 PowerShell repositories
-
1 Module Basics
-
Part 2 - Project and Module Design
-
6 Choosing a Module Layout
-
6.1 Monolithic PSM1
- 6.1.1 Module structure
-
6.2 Category submodules
- 6.2.1 Grouping by domain
-
6.3 Dot-sourced functions from PSM1
- 6.3.1 What happens during import
- 6.3.2 Advantages and disadvantages
- 6.4 Final verdict
- 6.5 Summary
-
6.1 Monolithic PSM1
-
7 Keeping Some Module Contents Private
-
7.1 Determining the public functions
- 7.1.1 The role of private functions
- 7.1.2 Candidates for private functions
- 7.1.3 Controlling the visibility of variables and aliases
- 7.2 Summary
-
7.1 Determining the public functions
-
8 Using Classes in a Module
-
8.1 Importing classes
- 8.1.1 The using statement
- 8.1.2 Differences from Import-Module
- 8.1.3 Differences from #requires
-
8.2 Using classes internally in a module
- 8.2.1 Trying to dot-source classes
- 8.3 Ordering classes
-
8.4 Extending classes from other modules
- 8.4.1 Having a module reference classes from another module
-
8.5 The user experience of classes
- 8.5.1 Providing functions to create class instances
- 8.6 Summary
-
8.1 Importing classes
-
9 Building a Module From Many Files
- 9.1 Benefits of a monolithic PSM1
- 9.2 Benefits of dot-sourced functions
- 9.3 Creating a dot-sourced module
-
9.4 Building a module - E pluribus enum (out of many, one)
- 9.4.1 Creating a project directory
- 9.4.2 Creating a build script
- 9.4.3 Running the build script
- 9.4.4 Exporting functions in the manifest
- 9.5 Summary
-
10 Versioning a Module
-
10.1 What is a software version
- 10.1.1 Examples of versioning schemes
-
10.2 Semantic Versioning
- 10.2.1 SemVer rules
- 10.2.2 Why SemVer is useful
-
10.3 Automating module versioning
- 10.3.1 Avoid auto-incrementing
- 10.3.2 Incrementing a module version
-
10.4 Pre-release versioning
- 10.4.1 Adding a pre-release moniker
- 10.4.2 Publishing a pre-release module
- 10.4.3 Finding and installing a pre-release module
- 10.5 Tips for avoiding breaking changes
-
10.6 Handling deprecation
- 10.6.1 Communication avenues
-
10.7 Specifying compatible operating systems and PowerShell editions
- 10.7.1 The CompatiblePSEditions module manifest field
- 10.8 Summary
-
10.1 What is a software version
-
11 Building Better Functions
-
11.1 Plan your dive, dive your plan
- 11.1.1 Planning your functions
- 11.1.2 Think about the experience
- 11.2 Functions should do one thing
- 11.3 Creating testable functions
- 11.4 Self-contained functions
-
11.5 Writing defensive functions
- 11.5.1 Enforce mandatory parameters
- 11.5.2 Explicitly define parameter types
- 11.5.3 Use validation attributes
- 11.6 Document your functions
- 11.7 Adhere to approved verbs
- 11.8 Use proper parameter names
- 11.9 Use advanced functions
- 11.10 Support the pipeline
-
11.11 Create safe functions
-
11.11.1 Adding -WhatIf and
-Confirm
to your functions - 11.11.2 Using the -Force
-
11.11.1 Adding -WhatIf and
-
11.12 Sensible error handling
- 11.12.1 Creating terminating errors
- 11.12.2 Creating non-terminating errors
-
11.13 Write with cross-platform in mind
- 11.13.1 Working with the PATH environment variable
- 11.13.2 File paths and delimiters
- 11.14 Summary
-
11.1 Plan your dive, dive your plan
-
12 Creating a Quality GitHub Project
-
12.1 The README
- 12.1.1 Elements of the README
- 12.1.2 Jazzing up the README with badges
- 12.2 Choosing a license
- 12.3 Keeping track of what changed
- 12.4 The Code of Conduct
- 12.5 Tell folks how to contribute
-
12.6 Issue and pull request templates
- 12.6.1 Template front matter and Markdown
- 12.6.2 Pull request template
-
12.7 Divvying up the work with CODEOWNERS
- 12.7.1 CODEOWNERS Syntax
- 12.8 Project sponsors and funding
- 12.9 Summary
-
12.1 The README
- 13 Documenting a Project
-
6 Choosing a Module Layout
- Part 3 - The Build and Test Loop
- Part 4 - Creating a Quality Community Project
-
Bonus Chapters
-
14 PowerShell Classes Explained
- 14.1 What is a PowerShell Class
- 14.2 Why use classes
- 14.3 Defining and creating a class
- 14.4 Adding class properties
- 14.5 Defining class methods
- 14.6 Initializing class properties
- 14.7 Validating properties by using attributes
- 14.8 Accessing the class members with $this
- 14.9 Passing input to methods
- 14.10 Method overloading and signatures
-
14.11 Making properties and methods static
- 14.11.1 Accessing static properties
- 14.11.2 Calling static methods
- 14.12 Initializing a class instance with constructors
-
14.13 Inheritance
- 14.13.1 Creating a derived class
- 14.13.2 Overriding methods
- 14.13.3 Calling base methods
- 14.13.4 Calling constructors
- 14.14 Hiding properties and methods
-
14.15 Enumerations
- 14.15.1 Using an enumeration
- 14.15.2 Using enumerations in parameters
- 14.15.3 Creating an enumeration
- 14.15.4 Flag enumerations
- 14.16 Summary
-
14 PowerShell Classes Explained
Other books by this author
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 earnedover $13 millionwriting, 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