Libraries#

PowerShell’s re-use story has three layers. Built-in cmdlets ship with the core modules and require no setup. Modules are the unit of distribution; folders with a .psd1 manifest and .psm1 script that Import-Module loads. The PowerShell Gallery is the public registry; Install-Module pulls from it. On top, the operator can .-source plain script files into the current scope.

Built-in Helpers#

The core modules cover the bread-and-butter operations.

Module

What it brings

Microsoft.PowerShell.Core

Get-Help, Get-Command, Import-Module, ForEach-Object, Where-Object

Microsoft.PowerShell.Management

Get-Process, Get-Service, Get-ChildItem, Copy-Item, Test-Path, Get-Content, Set-Content

Microsoft.PowerShell.Utility

Select-Object, Sort-Object, Group-Object, Measure-Object, Compare-Object, Format-*, ConvertTo-* / ConvertFrom-*, Out-*

Microsoft.PowerShell.Security

Get-Credential, ConvertTo-SecureString, Get-AuthenticodeSignature

Microsoft.PowerShell.Archive

Compress-Archive, Expand-Archive

PSReadLine

Interactive line editor (always loaded in interactive sessions)

ThreadJob

Start-ThreadJob (5.1 install-on-demand; built in 7+)

Cmdlet Quick Reference#

The cmdlets most operators reach for outside the obvious ones.

Cmdlet

Use it for

ConvertFrom-Json / ConvertTo-Json

JSON ↔ object

Import-Csv / Export-Csv

CSV ↔ object

Invoke-RestMethod

HTTP API call returning a parsed object

Invoke-WebRequest

HTTP call returning the raw response

Invoke-Command

Run a script block locally or on remote hosts

Start-Job / Start-ThreadJob / Wait-Job

Background work

Get-Random

Random number / element

New-TimeSpan

Build a TimeSpan for arithmetic with dates

Out-String / Out-File / Out-Null

Force formatting, write to file, discard

Tee-Object

Save pipeline output to a file and pass it on

Common Modules#

The third-party modules the everyday operator pulls in.

  • Az, the Azure cmdlet suite. Big; the operator usually imports only the sub-modules they need (Az.Compute, Az.Storage).

  • AWS.Tools.*, modular AWS cmdlets; one module per service.

  • Microsoft.Graph, Microsoft 365 / Entra ID / Intune over Graph API.

  • Pester, testing framework.

  • PSScriptAnalyzer, linter.

  • PSReadLine, line editor.

  • ImportExcel, read / write .xlsx without Excel installed.

  • PSFramework, logging, config, runspace pools.

Building a Module#

A module is a folder.

MyModule/
  MyModule.psd1          # manifest (metadata)
  MyModule.psm1          # main script (entry point)
  Public/                # one .ps1 per public function
    Get-Thing.ps1
  Private/               # internal helpers
    Helper.ps1

The .psm1 typically dot-sources every .ps1 under Public and Private and exports the public names.

# MyModule.psm1
$public  = @( Get-ChildItem $PSScriptRoot/Public/*.ps1 -ErrorAction SilentlyContinue )
$private = @( Get-ChildItem $PSScriptRoot/Private/*.ps1 -ErrorAction SilentlyContinue )

foreach ($file in $public + $private) {
    . $file.FullName
}
Export-ModuleMember -Function $public.BaseName

Generate a manifest.

New-ModuleManifest -Path ./MyModule.psd1 `
    -RootModule MyModule.psm1 `
    -ModuleVersion 0.1.0 `
    -Author 'operator' `
    -FunctionsToExport 'Get-Thing'

Publish.

Publish-Module -Path ./MyModule -NuGetApiKey $env:PSGALLERY_KEY

Dot-Sourcing a Script#

For ad-hoc re-use without packaging, dot-source a script into the current scope. Every function and variable it defines is available afterwards.

. ./helpers.ps1
Get-MyHelper

The single leading dot is significant; without it, the script runs in its own scope and the names vanish on return.

References#