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 references 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 Elements of a Quality GitHub Project
- 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 a enumeration
- 14.15.2 Using enumerations in parameters
- 14.15.3 Creating a enumeration
- 14.15.4 Flag enumerations
- 14.16 Summary
-
14 PowerShell Classes Explained
Other books by this author
Authors have earned$10,089,287writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
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), EPUB (for phones and tablets) and MOBI (for 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
Top Books
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
The book is almost daily updated. These incremental updates ease my interaction with the proofreaders.
A Guide to Artificial Intelligence in Healthcare
Dr. Bertalan MeskoCan we stay human in the age of A.I.? To go even further, can we grow in humanity, can we shape a more humane, more equitable and sustainable healthcare? This e-book aims to prepare healthcare and medical professionals for the era of human-machine collaboration. Read our guide to understanding, anticipating and controlling artificial intelligence.
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
Sockets and Pipes
Type ClassesSockets and Pipes is not an introduction to Haskell; it is an introduction to writing software in Haskell. Using a handful of everyday Haskell libraries, this book walks through reading the HTTP specification and implementing it to create a web server.
Introducing EventStorming
Alberto BrandoliniThe deepest tutorial and explanation about EventStorming, straight from the inventor.
node-opcua by example
Etienne RossignonGet the best out of node-opcua through a set of documented examples by the author himself that will allow you to create stunning OPCUA Servers or Clients.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Functional Design and Architecture
Alexander GraninSoftware Design in Functional Programming, Design Patterns and Practices, Methodologies and Application Architectures. How to build real software in Haskell with less efforts and low risks. The first complete source of knowledge.
Composing Software
Eric ElliottAll software design is composition: the act of breaking complex problems down into smaller problems and composing those solutions. Most developers have a limited understanding of compositional techniques. It's time for that to change.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #3
PowerShell
3 Books
Buy every PowerShell book from Adam Bertram at a 20% discount! - #4
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #5
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #6
Modern C++ by Nicolai Josuttis
2 Books
- #7
Django for Beginners/APIs/Professionals
3 Books
- #8
Learn Git, Bash, and Terraform the Hard Way
3 Books
Learn Git, Bash and Terraform using the Hard Way method.These technologies are essential tools in the DevOps armoury. These books walk you through their features and subtleties in a simple, gradual way that reinforces learning rather than baffling you with theory. - #9
Software Architecture and Beautiful APIs
2 Books
There is no better way to learn how to design good APIs than to look at many existing examples, complementing the Software Architecture theory on API design. - #10
Digital Future of Healthcare and Pharma
3 Books
These three popular e-books from The Medical Futurist describes how digital health technologies will shape the future of health, healthcare, medicine and pharma with exciting infographics in a digestible format.