PowerShell Scripting Concepts and Vocabulary
This guide defines the core vocabulary and concepts required for effective scripting and programming in PowerShell 7+ (Core). Unlike traditional shell scripting which manipulates text, PowerShell is built on .NET and manipulates objects.
Core Execution Units
Cmdlet (Command-let)
A lightweight command used in the PowerShell environment. The PowerShell runtime invokes these in the context of automation scripts.
- Structure: Always follows a
Verb-Nounnaming convention (e.g.,Get-Process,New-Item). - Origin: Typically compiled .NET classes (C#).
Function
A named block of PowerShell code that performs a specific task. Functions behave similarly to cmdlets but are written in the PowerShell language rather than compiled languages.
- Advanced Functions: Functions that use the
[CmdletBinding()]attribute to access common parameters (like-Verboseor-WhatIf) and pipeline input.
ScriptBlock
A collection of statements enclosed in braces { ... }. It is essentially an anonymous function or lambda expression.
- Usage: Used heavily in pipeline operations (e.g.,
Where-Object { $_.Name -eq 'svchost' }) or passed as parameters.
Alias
An alternate name or nickname for a cmdlet or function (e.g., ls for Get-ChildItem, gc for Get-Content).
- Best Practice: Avoid using aliases in saved scripts for readability and maintainability.
Data & Objects
Object
PowerShell is object-oriented. An object is a discrete entity that contains data (Properties) and instructions (Methods). When you run Get-Process, you don't get text; you get instances of System.Diagnostics.Process.
Member
Refers to the properties, methods, and events associated with an object.
- Discovery: Use
Get-Member(aliasgm) to inspect the type and members of an object.
Property
Data stored within an object (e.g., a Process object has an Id property and a Name property).
Method
Actions that can be performed on or by an object (e.g., a Process object has a .Kill() method).
PSCustomObject
A generic object type used to create structured data on the fly.
$obj = [PSCustomObject]@{
Name = "Server01"
IP = "192.168.1.10"
}
The Pipeline
Pipeline (|)
The mechanism used to pass the output object of one command as the input object of the next command.
- ByValue: Matches the incoming object type to the parameter type.
- ByPropertyName: Matches a property name on the incoming object to a parameter name on the receiving command.
Language Elements
Variable
A unit of memory used to store values. In PowerShell, variables are denoted by the $ prefix (e.g., $MyVar).
- Typing: PowerShell is dynamically typed but allows for strong typing (e.g.,
[int]$Number = 5).
Scope
Determines the visibility and accessibility of variables and functions.
- Global: Available everywhere in the current session.
- Script: Available anywhere within the script file.
- Local: Available only in the current block (function or loop).
- Private: Not visible to child scopes.
Splatting
A method of passing a collection of parameter values to a command as a unit (usually a Hashtable). This improves readability.
$params = @{
Path = "C:\Temp"
Recurse = $true
Force = $true
}
Get-ChildItem @params
Data Structures
Array (@())
A collection of items. PowerShell arrays are zero-indexed.
$arr = @(1, 2, 3)
Hash Table (@{})
A dictionary collection of key-value pairs.
$hash = @{ Key1 = "Value1"; Key2 = "Value2" }
Modularization
Module
A package that contains PowerShell members, such as cmdlets, providers, functions, variables, and aliases.
- .psm1: The script module file containing the code.
- .psd1: The module manifest file containing metadata (version, author, exported commands).
Dot Sourcing (. .\Script.ps1)
Running a script in the current scope rather than a child scope. This allows the script to modify variables or define functions that persist in your current session after the script finishes.
Environment
PSProvider
An adapter that makes data stores look like file system drives.
- Examples: Registry, Certificate Store, Environment Variables, FileSystem.