Splatting | Allows passing parameters to a command as a hashtable or array, improving readability and maintainability of code. | <command> @<splatName> | powershell<br>$params = @{<br> Name = "Notepad"<br> CPU = 100<br>}<br>Get-Process @params<br> |
Scriptblocks | A powerful way to define reusable blocks of code that can be executed later or passed around. | $scriptblock = { <code> } | powershell<br>$sb = { Get-Date }<br>Invoke-Command -ScriptBlock $sb<br> |
Dynamic Parameters | Allows the creation of parameters that only appear based on certain conditions, enhancing cmdlet flexibility. | DynamicParam { <code> } | powershell<br>function Get-Data {<br> DynamicParam {<br> # Code to define parameters dynamically<br> }<br>}<br> |
Advanced Functions (CmdletBinding) | Creates functions that behave like cmdlets, with support for features like parameter validation, pipeline input, and more. | function <name> { [CmdletBinding()] param (<params>) <code> } | powershell<br>function Get-Sum {<br> [CmdletBinding()]<br> param (<br> [Parameter(Mandatory=$true)]<br> [int]$a,<br> [int]$b<br> )<br> return $a + $b<br>}<br> |
Pipelines and Custom Pipeline Functions | Enables passing output from one command as input to another, and creating functions that support pipeline input. | “`<command1> | <command2>“` |
Background Jobs | Executes commands or scripts asynchronously in the background, allowing other tasks to continue without waiting for completion. | Start-Job -ScriptBlock { <code> } | powershell<br>$job = Start-Job -ScriptBlock { Get-Process }<br>Receive-Job -Job $job<br> |
Workflows | Enables the creation of workflows that can be resumed, suspended, and parallelized, useful for long-running or complex tasks. | workflow <name> { <code> } | powershell<br>workflow MyWorkflow {<br> Parallel {<br> Get-Process<br> Get-Service<br> }<br>}<br> |
Error Handling with Try-Catch-Finally | Provides robust error handling to manage exceptions and ensure the script can handle unexpected situations gracefully. | try { <code> } catch { <code> } finally { <code> } | powershell<br>try {<br> $result = 1 / 0<br>} catch {<br> Write-Output "Error: $_"<br>} finally {<br> Write-Output "Cleanup actions"<br>}<br> |
Modules and Script Module Creation | Organizes and reuses code by packaging functions and scripts into modules, which can be easily imported into other scripts. | New-Module -ScriptBlock { <functions> } | powershell<br>function Get-Data {<br> param ($source)<br> # Fetch data<br>}<br>Export-ModuleMember -Function Get-Data<br> |
Remoting with Invoke-Command | Executes scripts or commands on remote computers, enabling centralized management and automation across multiple systems. | Invoke-Command -ComputerName <name> -ScriptBlock { <code> } | powershell<br>Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process }<br> |
Desired State Configuration (DSC) | Ensures that a system’s configuration stays consistent with a desired state, automating setup and maintenance tasks. | Configuration <name> { <code> } | powershell<br>Configuration MyConfig {<br> Node "Server01" {<br> WindowsFeature "WebServer" {<br> Ensure = "Present"<br> Name = "Web-Server"<br> }<br> }<br>}<br> |
Implicit Remoting | Imports remote session cmdlets into the local session, allowing you to work with remote resources as if they were local. | Import-PSSession (New-PSSession -ComputerName <name>) | powershell<br>$session = New-PSSession -ComputerName Server01<br>Import-PSSession $session<br> |
CIM and WMI Commands | Allows management and interaction with Windows Management Instrumentation (WMI) and Common Information Model (CIM) resources. | Get-CimInstance -ClassName <class> | powershell<br>Get-CimInstance -ClassName Win32_OperatingSystem<br> |
Leveraging .NET Classes | Accesses and uses .NET framework classes directly within PowerShell, allowing for more advanced programming and automation. | [Namespace.Class]::Method() | powershell<br>[System.DateTime]::Now<br> |
PowerShell Classes and Objects | Defines custom classes and objects, enabling object-oriented programming within PowerShell. | class <name> { <properties and methods> } | powershell<br>class Person {<br> [string]$Name<br> [int]$Age<br> Person([string]$name, [int]$age) {<br> $this.Name = $name<br> $this.Age = $age<br> }<br>}<br>$p = [Person]::new("John", 30)<br>$p.Name<br> |
Reflection and Metadata Manipulation | Allows inspection and modification of object metadata and types at runtime, enabling dynamic and flexible scripting. | [reflection]::GetMethod() | powershell<br>[reflection.assembly]::LoadWithPartialName("System.Reflection")<br> |