PowerShell Core Concepts
PowerShell is not just a command-line tool; its design philosophy, functional architecture, and usage differ significantly from traditional shells.
I. Cmdlet: The Smallest Command Unit
What is a Cmdlet?
Cmdlet (pronounced as command-let) is the most basic command unit in PowerShell; they are .NET-based classes that return one or more .NET objects.
Naming convention: verb-noun
Every Cmdlet follows the verb-noun naming convention; for example:
| Cmdlet | Meaning |
|---|---|
Get-Process |
Get process information |
Set-Item |
Set the value of a resource item |
Remove-Item |
Delete files, registry keys, etc. |
New-User |
Create a new user (if the AD module is installed) |
PowerShell ships with hundreds of Cmdlets, and third-party modules can define even more.
Example
Get-Service
Get-ChildItem -Path C:\Windows
You can also use the Get-Command View all available Cmdlets:
Get-Command -CommandType Cmdlet
II. Object Pipeline
What is the difference from UNIX/Linux pipes?
In a traditional shell, the pipe (|) passes text strings. In PowerShell,the pipeline passes complete .NET objects.
which means you can preserve structured data (properties, methods) for further processing.
Example: Filter processes by CPU usage
Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object Name, CPU
In the command above:
Get-ProcessGet all processes (returns objects)Where-ObjectFilter processes with CPU usage greater than 100Select-ObjectOutput onlyNameandCPUField
The beauty of passing values through the pipeline
You do not need to use awk, cut, grep and other tools to 'parse' the output; instead you operate directly on object properties.
III. Provider and PSDrive: Resource Drive Abstraction
PowerShell provides a unified way to access resources:the Provider model, which maps various resources to virtual drives (PSDrive), just like C: a disk.
Common Provider Types
| Provider | Example drive | Description |
|---|---|---|
| FileSystem | C:, D: |
Local file system |
| Registry | HKLM:, HKCU: |
Windows Registry |
| Environment | Env: |
Environment Variables |
| Certificate | Cert: |
Certificate store |
| Function | Function: |
Functions in the current session |
| Variable | Variable: |
Currently defined variables |
| Alias | Alias: |
Command aliases |
Example: Working with the registry
Get-ChildItem HKLM:\Software\Microsoft
Browse the registry just like browsing a folder.
IV. Scripts and Modules: Organization and Reuse
Scripts (.ps1)
- A PowerShell script is a text file with the
.ps1extension - containing a series of commands, flow control, functions, and so on
Modules (.psm1 / .psd1)
A module is a functional unit for reuse in PowerShell; it can be:
- A
.psm1a file (module script) - a folder containing a
.psd1manifest (advanced module)
A module can support:
- automatic loading(loaded automatically when its functions are used)
- Versioning
- dependency declaration
View all modules in the system:
Get-Module -ListAvailable
Import a module:
Import-Module Az
V. Execution Policy
To prevent the execution of malicious scripts, PowerShell introduced the execution policy(Execution Policy), which restricts .ps1 the execution behavior of script files.
Common policy types
| Strategy | Meaning |
|---|---|
| Restricted | Default: do not allow any scripts to run |
| RemoteSigned | Allow local scripts to run; remote scripts must be signed |
| AllSigned | All scripts must be signed |
| Bypass | Unrestricted; not recommended in production |
Set the execution policy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Note: The execution policy only affects script files and does not restrict commands typed manually.
VI. Remote Management (PowerShell Remoting)
PowerShell Remoting supports connecting via WS-MAN (based on WinRM) or SSH protocol to remotely connect to another computer and execute commands.
Enable WinRM (on Windows)
Enable-PSRemoting -Force
Common Commands
# 建立远程会话
Enter-PSSession -ComputerName Server01
# Batch execute commands
Invoke-Command -ComputerName Server01,Server02 -ScriptBlock { Get-Service }
Cross-platform remoting with SSH support
Enter-PSSession -HostName linux01 -User user1 -SSHTransport
VII. Desired State Configuration (DSC)
DSC (Desired State Configuration) is PowerShell'sdeclarative configuration platform, used to define the system's 'desired state'.
It allows you to:
- Define resource state with a configuration script (e.g., a service should be running, a file should exist)
- Automatically configure the machine to that state
- Detect drift and automatically remediate
Usage
- Write a configuration file (
.ps1) - compile it into a MOF file
- deploy it using Push or Pull mode
DSC is especially suitable for large-scale server configuration, compliance checks, and automated deployment.
VIII. Command Discovery and Help System
PowerShell provides a rich help system that lets you easily explore commands:
Get-Help Get-Process: View command helpGet-Command: List all commandsGet-Member: View object properties and methodsGet-Help about_*: View built-in knowledge documents (such asabout_Execution_Policies)
Example: Inspect an object's structure
Get-Process | Get-Member
Summary
| Concept | Keyword | Brief description | |
|---|---|---|---|
| Cmdlet | 动词-名词 |
Smallest execution unit, based on .NET | |
| Pipeline | ` | ` | Passes objects rather than text |
| Provider | FileSystem, Env: |
Unified access to various resources | |
| module | .psm1, .psd1 |
Organize reusable command collections | |
| execution policy | Set-ExecutionPolicy |
Control script execution permissions | |
| Remote management | Enter-PSSession |
Execute commands remotely | |
| DSC | Configuration |
Declarative system configuration platform | |
| Help system | Get-Help |
Built-in documentation system |