Overview

7 Adding commands

PowerShell’s strength lies in its extensibility: you can “add commands” by installing modules, much like adding apps to a phone. Modern PowerShell (v6+) focuses on modules rather than the older snap-ins, and Microsoft as well as the community publish modules for services like Azure Compute, Networking, DNS, and more. PowerShellGet streamlines discovering, installing, and updating these extensions from online repositories such as the PowerShell Gallery, though you should treat community code with appropriate caution.

The chapter shows how to find and install modules with commands like Find-Module and Install-Module, handle repository trust prompts, and verify installations with Get-Module -ListAvailable. It explains PSModulePath, the environment variable that tells PowerShell where to look for modules, how to add custom locations to it, and why keeping it lean improves autodiscovery and autoloading of commands and help. If a module lives outside those paths, you can import it by full path; modules may also add providers to the shell. A notable exception discussed is the GoogleCloud module, which requires an initial Import-Module -Force to complete setup before its commands become discoverable.

To avoid command conflicts, most vendors prefix nouns (for example, Az or Gc), and you can disambiguate collisions by module-qualified names or remove a module; you can also assign a custom prefix during import. The text models a practical exploration workflow using the Microsoft.PowerShell.Archive module, demonstrating Compress-Archive and Expand-Archive with parameters like -Verbose and -Force, and emphasizes using Get-Help (especially -Examples or -Full) to learn new commands efficiently. A concluding lab reinforces these skills: browse and install modules, import them, archive and expand files, and verify results with Compare-Object.

A screenshot of a cell phone Description automatically generated
A screenshot of a cell phone Description automatically generated
Graphical user interface, text, application, email, Teams Description automatically generated

 Lab answers

Here’s one way to approach this:

  1. Install-Module moduleyoufound
    a)   If you are on a Windows machine we suggest the import-excel module
  2. Get-Command –module moduleyoufound
    a)   Get-command -module az
  3. Find-Module -Command Compress-Archive | Install-Module -Force
  4. Import-Module Microsoft.PowerShell.Archive
  5. will create a collection of the numbers between 1 and 10. If you did this another way, don’t worry.
    a)   New-Item ~/TestFolder -ItemType Directory
    b)   1..10 | ForEach-Object {New-Item "~/TestFolder/$_.txt" -ItemType File -Value $_}
  6. Compress-Archive ~/TestFolder/* -DestinationPath ~/TestFolder.zip
  7. Expand-Archive ~/TestFolder.zip -DestinationPath ~/TestFolder2
  8. This is a possible way. Remember dir is an alias for Get-ChildItem

FAQ

What is a PowerShell module and how does it add commands?A module is a self-contained package that adds commands, help, and other assets to PowerShell. Starting with PowerShell 6, modules are the only supported extension type (snap-ins are legacy). Installing a module makes its commands discoverable and auto-loadable by the shell.
How do I find and install modules from the PowerShell Gallery?Use PowerShellGet. Common commands include: Find-Module (search), Install-Module (install), and Update-Module (update). On first install you may see an “untrusted repository” prompt; use Set-PSRepository to change the policy if appropriate.
Is everything in the PowerShell Gallery Microsoft-produced or endorsed?No. The Gallery hosts community-contributed code. Review, test, and use due caution before running third‑party modules in your environment.
How can I check whether a module supports my PowerShell edition or platform?On the module’s page in the PowerShell Gallery, review the Package Details and PSEditions section to see what the author has tested and supports.
What is PSModulePath and how do I view or modify it?PSModulePath is an environment variable listing folders where PowerShell looks for modules. View it with (Get-Content Env:/PSModulePath) and split by ':' on macOS/Linux or ';' on Windows. Add a path with $env:PSModulePath += [System.IO.Path]::PathSeparator + 'C:\Scripts\MyModules'.
How does module autodiscovery and autoloading work?If a module resides in any folder listed in PSModulePath, PowerShell can discover its commands, show help for them, and autoload the module the first time you run one of its commands. Keep PSModulePath lean for best performance.
What if my module isn’t in a PSModulePath location?Import it by specifying the full path, for example: Import-Module 'C:\Scripts\MyModules\MyModule'. You can also add that folder to PSModulePath so it’s found automatically next time.
How do I resolve command name conflicts between modules?Prefer modules that use product-specific noun prefixes (for example, Get-AzStorageTable or Get-GcSqlInstance). To disambiguate, run a command as ModuleName\CommandName, import a module with a custom prefix using Import-Module -Prefix, or unload a module with Remove-Module.
Why don’t I see any commands after installing a module like GoogleCloud?Some modules require a first‑run initialization. Run Import-Module GoogleCloud -Force to trigger setup; after that, commands (for example, those managing Google Cloud SQL) will appear.
How can I quickly learn a new module’s commands (example: archive/zip)?Use Get-Command -Module ModuleName to list commands, then Get-Help with -Full or -Examples. For zipping, the Microsoft.PowerShell.Archive module provides Compress-Archive and Expand-Archive; use -Path, -DestinationPath, -Force to overwrite, and -Verbose to see progress.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free