Lesson 11 of 13 · 15m
Module Discipline and the Gallery
PSGallery is unvetted public infrastructure: stage, review, pin versions, and serve production from a private feed.
A PowerShell module is a folder containing a manifest — Name.psd1, a versioned hashtable of metadata — plus the code (Name.psm1 script module, a compiled .dll, or both). The manifest decides what the module exports, what it requires, and which version it claims to be. $env:PSModulePath decides where PowerShell looks: on 5.1 that's ~\Documents\WindowsPowerShell\Modules per user, C:\Program Files\WindowsPowerShell\Modules machine-wide, and the in-box System32\WindowsPowerShell\v1.0\Modules; on PowerShell 7 the folders swap WindowsPowerShell for PowerShell and add ~\Documents\PowerShell\Modules. Get-Module -ListAvailable shows everything discovered across all of them.
The manifest is the contract
New-ModuleManifest writes the .psd1. The fields that matter in production: ModuleVersion (SemVer-ish, drives side-by-side loading), RootModule, FunctionsToExport, CmdletsToExport, RequiredModules, PowerShellVersion. FunctionsToExport = '*' is the lazy default everyone ships — it exports everything and it costs autoload performance because PowerShell can't index the module without loading it to discover commands. An explicit export list is both faster (Get-Command resolves via the manifest cache) and a smaller attack surface — unexported helpers stay internal. Test-ModuleManifest validates the file before it ever reaches a server.
Side-by-side versioning is real: Modules\MyTools\1.4.0\ and Modules\MyTools\2.0.0\ coexist, and Import-Module MyTools -RequiredVersion 1.4.0 pins the load. -MaximumVersion/-MinimumVersion bracket it when you need a range. This is how you roll a fleet forward gradually instead of big-banging a breaking change — but only if your scripts actually declare versions, which brings us to the discipline nobody practices.
The Gallery is a publishing pipeline, not a vetting pipeline
Install-Module -Name Pester -Scope CurrentUser pulls from PSGallery, a public feed where anyone with an account can publish. Install-Module fetches and installs in one step. Save-Module -Path .\staging downloads without installing — the distinction that matters, because installing to prod straight from a public feed is a supply-chain decision disguised as a convenience.
Aqua Security published the receipts in August 2023: PSGallery allowed typosquat names indistinguishable from real modules, let anyone spoof the Author field, and — worst — served unlisted packages directly to anyone who guessed the name, so abandoned-but-removed modules remained installable. Az-adjacent name squatting, lookalike Azure.* packages — the mechanism is boring and it works, because Install-Module does not verify anything beyond "does the name exist." Set-PSRepository PSGallery -InstallationPolicy Untrusted forces a prompt, which at least makes you read the name you're about to run.
PowerShellGet v2 (Install-Module, Find-Module, Save-Module) is what 5.1 ships and what most scripts use. Microsoft.PowerShell.PSResourceGet is its replacement — Install-PSResource, -TrustRepository, -Reinstall, proper dependency handling — and ships with PS 7.4+. Both hit the same feed; only the syntax changes.
The internal-repo pattern
The mature shape: a private repository — a NuGet feed on ProGet, Azure Artifacts, or a file share — registered via Register-PSRepository -Name Corp -SourceLocation ... -InstallationPolicy Trusted. Vetted modules get published there (Publish-Module -Repository Corp or Publish-PSResource), prod servers have PSGallery untrusted or unregistered entirely, and Install-Module -Repository Corp only sees what you approved. Staging a new upstream module is then Save-Module → review the source → commit the approved version to the private feed → pin -RequiredVersion in the consuming automation. The version pin is the contract that keeps next Tuesday's upstream release from becoming your incident.
Audit what you already have: Get-InstalledModule lists everything PowerShellGet installed (the InstalledLocation column tells you scope); compare against a manifest of what's supposed to be there. Modules that appeared on a server but not through your feed are a finding, not a curiosity.
Autoload and the cost of magic
Module autoload resolves Get-ADUser without Import-Module by scanning manifests for exported commands — it works because manifests declare FunctionsToExport/CmdletsToExport explicitly. When it "can't find the module," the command name resolution order is where you look: Get-Command Get-ADUser might be resolving to a different module than you think — same name exported by two installed modules, first one on PSModulePath wins. Get-Command -All shows every binding. A shadowed command that's almost right is worse than a missing one: it runs.
The practice
For code you ship: explicit FunctionsToExport, ModuleVersion bumped per change, RequiredModules declared instead of assumed. For code you consume: untrusted gallery, Save-Module → review → private feed → Import-Module -RequiredVersion in automation. And once a quarter, Get-InstalledModule on the fleet against a known-good list — because the module that didn't come through your pipeline is the one that came through someone else's.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
A Fake GTA 6 ISO Didn’t Install a Game. It Installed a Wiper.
A fake GTA 6 ISO used a believable installer error to cover remote-access malware, browser-data theft, and destructive file wiping. The “License not found” message wasn’t the crack failing. It was there so the victim would stop looking.
BlueHammer: The Unpatched Windows Zero-Day That Weaponizes Microsoft Defender Against Your Own System
BlueHammer is an unpatched, publicly released Windows local privilege escalation exploit that chains Microsoft Defender's update workflow, Volume Shadow Copy, Cloud Files callbacks, and opportunistic locks to reach NT AUTHORITY\SYSTEM from a standard user account.
Running a Minecraft Network: Four JVMs, One Box, and the Settings That Lie to You
Four servers on one host means four JVMs each sizing its thread pools for a machine it does not own. The measured cost of that, the systemd ordering that guarantees nothing, and the throttle that switches itself back on when your forwarding breaks.
Building a Minecraft Network: Four Servers Pretending to Be One Place
A proxy, a lobby, an SMP and a PvP arena is four JVMs that know nothing about each other. What actually has to be shared, why your permissions don't follow players, and the two LuckPerms defaults that silently share nothing at all.