BudiBadu Logo
Samplebadu
PowerShell

Learn PowerShell by Examples

PowerShell 7

PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework. It runs on Windows, Linux, and macOS.

These examples cover the core concepts of PowerShell, from its object-oriented pipeline and variable types to advanced features like error handling and JSON manipulation.

Cmdlets

Understanding PowerShell commands with this sample code demonstrating Verb-Noun naming convention, common cmdlets for system management, parameter usage with named arguments, and object output from cmdlet execution.

Pipeline

Chaining commands with object passing through this code example showing pipe operator for command connection, object property access in pipeline, parameter binding mechanisms, and streaming object processing.

Variables

Storing and manipulating data with this sample code demonstrating dollar sign variable syntax, dynamic typing with optional type constraints, string interpolation in double quotes, scope modifiers, and variable cmdlets.

Arrays

Managing collections of items with this code example demonstrating array creation using @() syntax and comma operator, zero-based indexing with negative indices, array slicing with range operator, and ArrayList for dynamic sizing.

Hashtables

Working with key-value pairs through this sample code demonstrating hashtable creation with @{} syntax, dot notation and bracket notation for value access, ordered hashtables for insertion order preservation, and key enumeration.

Functions

Creating reusable code blocks with this code example showing function definition with param block, advanced functions using CmdletBinding attribute, parameter attributes for validation, and begin-process-end blocks for pipeline support.

Modules

Organizing and distributing PowerShell code with this sample code demonstrating module import, command discovery within modules, module creation with .psm1 files, Export-ModuleMember for public interfaces, and PowerShell Gallery integration.

Error Handling

Managing exceptions and errors with this code example demonstrating try-catch-finally blocks for structured error handling, ErrorAction parameter for error behavior control, specific exception type catching, and automatic error variable access.

Objects

Working with .NET objects and custom objects through this sample code demonstrating PSCustomObject creation, Add-Member for dynamic properties, Select-Object with calculated properties, Get-Member for object inspection, and property access methods.

JSON

Converting between objects and JSON format with this sample code demonstrating ConvertTo-Json for serialization with depth control, ConvertFrom-Json for deserialization to PSCustomObject, property access on parsed JSON, and file-based JSON operations.

Classes and Enums

Implementing object-oriented programming with this code example demonstrating class definition with typed properties, constructors, methods, inheritance, and enumerations for structured data types.

Package Management

Managing external dependencies with this code example demonstrating module discovery with Find-Module, installation from repositories, repository registration, and version control of installed packages.

Archive Cmdlets

Working with ZIP files using native cmdlets with this code example demonstrating compressing files and folders, updating existing archives, and expanding archives to destination directories.

Text Parsing

Extracting and manipulating text data using Select-String, regular expressions, and string operators with this code example for efficient log analysis and data processing.

System Information Cmdlets

Retrieving detailed hardware and operating system information using Get-ComputerInfo and CIM cmdlets with this code example for system auditing and monitoring.

Help System Updates

Maintaining up-to-date documentation with Update-Help and managing offline help content using Save-Help with this code example for disconnected environments.

Execution Policies

Understanding and managing script execution security with this code example demonstrating policy scopes, setting policies, and unblocking downloaded files.

Scheduled Jobs

Automating background tasks with this code example demonstrating job registration, triggers, options, and result retrieval using the PSScheduledJob module.

Module Manifests

Creating and managing module metadata with this code example demonstrating New-ModuleManifest, defining dependencies, and exporting module members.