Functions, Scripts and Modules

Harry · 13 Sep 2026 · 1 views

Functions

function Get-UserSummary {
    param(
        [string]$Name,
        [int]$Limit = 100
    )
    Get-User -Name $Name -Top $Limit | Select-Object Name, Email
}

Functions reuse logic; verb-noun names keep them cmdlet-like.

Scripts (.ps1)

# backup.ps1
$stamp = Get-Date -Format 'yyyyMMdd'
Copy-Item C:data D:ackupsdata-$stamp -Recurse
Write-Output "Backup done: $stamp"
.ackup.ps1

Scripts chain cmdlets into repeatable tasks, with param blocks at the top controlling inputs.

Error Handling

try {
    Get-Content missing.txt -ErrorAction Stop
} catch {
    Write-Warning $_.Exception.Message
} finally {
    Cleanup-Buffers
}

Modules

Modules package functions for reuse. Create a folder named after the module in $PSModulePath, export functions, and Import-Module it anywhere.

Key Points

  • Functions take params and stay testable.
  • .ps1 scripts turn workflows into repeatable automation.
  • try/catch with -ErrorAction Stop handles failures.
  • Modules distribute reusable function sets.
Share this post:

Comments (0)

Please login or register to comment.