Functions, Modules & Profiles
Package reusable PowerShell into functions and modules, and customize your profile.
Reusable PowerShell starts with functions that take typed parameters. Group
related functions into a module (.psm1) so they can be imported and shared.
The community registry, the PowerShell Gallery, distributes modules you install
with Install-Module.
Your profile script runs at every session start — the place for aliases,
default parameters, and helper functions you want always available. $PROFILE
holds its path.
function Get-DiskFreeGB {
[CmdletBinding()]
param(
[Parameter(Mandatory)] [string] $Drive
)
$d = Get-PSDrive $Drive
[math]::Round($d.Free / 1GB, 1)
}
Get-DiskFreeGB -Drive C # e.g. 84.3
# Install and use a community module
Install-Module Az -Scope CurrentUser
Import-Module Az
# Add to your profile
notepad $PROFILE # then add aliases/functions - Write a function with a mandatory parameter and call it.
- Add
[CmdletBinding()]and useWrite-Verbosewith the-Verboseswitch. - Install a module from the PowerShell Gallery and run one of its cmdlets.
- Add a custom alias to your
$PROFILEand reload the session.
Cheat Sheet▾
| Task | Command |
|---|---|
| Define function | function Verb-Noun { param(...) } |
| Mandatory param | [Parameter(Mandatory)] |
| Cmdlet behavior | [CmdletBinding()] |
| Import module | Import-Module Name |
| Install module | Install-Module Name |
| Profile path | $PROFILE |
| List modules | Get-Module -ListAvailable |
Common Interview Questions▾
What is a PowerShell module?
A package of related functions, cmdlets, and variables (often a .psm1 file or gallery package) that you import to reuse and share functionality.
What is the $PROFILE and why use it?
A script that runs at the start of every session — used to load aliases, functions, and default settings so your environment is consistent each time.