Powershell Core
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?
Cmdlets (pronounced _command-let_) are the most basic command units in PowerShell. They are all .NET-based classes that return one or more **.NET objects** upon execution.
### 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 comes with hundreds of Cmdlets, and third-party modules can define even more.
### Example
Get-ServiceGet-ChildItem -Path C:\Windows
You can also use `Get-Command` to view all available Cmdlets:
Get-Command -CommandType Cmdlet
* * *
## II. Object Pipeline
### What's the difference from UNIX/Linux pipes?
In traditional Shells, the pipe (`|`) passes text strings. In PowerShell, **the pipeline passes complete .NET objects**.
This means you can retain structured data (properties, methods) for subsequent processing.
### Example: Filter processes by CPU usage
Get-Process | Where-Object { $_.CPU -gt 100 } | Select-Object Name, CPU
In the above command:
* `Get-Process` gets all processes (returns objects)
* `Where-Object` filters processes with CPU usage greater than 100
* `Select-Object` outputs only the `Name` and `CPU` fields
### The Beauty of Pipeline Passing Values
You don't need to use tools like `awk`, `cut`, `grep`, etc., to "parse" the output; instead, you directly manipulate 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 (PSDrives), allowing you to operate them just like the `C:` drive.
### 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: Manipulating the Registry
Get-ChildItem HKLM:\Software\Microsoft
Browse the registry just like browsing folders.
* * *
## IV. Scripts and Modules: Organization and Reuse
### Scripts (.ps1)
* PowerShell scripts are text files with the `.ps1` extension
* They contain a series of commands, flow control, functions, etc.
### Modules (.psm1 / .psd1)
Modules are reusable functional units in PowerShell, which can be:
* A `.psm1` file (module script)
* A folder containing a `.psd1` manifest (advanced module)
Modules support:
* **Auto-loading** (automatically loaded when a function within it is used)
* **Version control**
* **Dependency declaration**
View all modules in the system:
Get-Module -ListAvailable
Import a module:
Import-Module Az
* * *
## V. Execution Policy
To prevent malicious script execution, PowerShell introduces **Execution Policy**, which restricts the execution behavior of `.ps1` script files.
### Common Policy Types
| Policy | Meaning |
| --- | --- |
| Restricted | Default, does not allow any scripts to run |
| RemoteSigned | Allows local scripts to run; remote scripts must be signed |
| AllSigned | All scripts must be signed |
| Bypass | Unrestricted, not recommended for use in production |
### Set Execution Policy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
> β οΈ Note: Execution policy only affects script files; it does not restrict manually entered commands.
* * *
## VI. PowerShell Remoting
PowerShell Remoting supports remotely connecting to another computer and executing commands via the **WS-MAN (based on WinRM)** or **SSH** protocol.
### Enable WinRM (on Windows)
Enable-PSRemoting -Force
### Common Commands
# Establish a remote sessionEnter-PSSession -ComputerName Server01# Execute commands in bulkInvoke-Command -ComputerName Server01,Server02 -ScriptBlock { Get-Service }
### Cross-platform remoting via SSH
Enter-PSSession -HostName linux01 -User user1 -SSHTransport
* * *
## VII. Desired State Configuration (DSC)
DSC (Desired State Configuration) is PowerShell's **declarative configuration platform**, used to define the "ideal state" of a system.
It allows you to:
* Define resource states using configuration scripts (e.g., a certain service should be running, a certain file should exist)
* Automatically configure the machine to that state
* Detect drift and automatically remediate
### Usage
1. Write a configuration file (`.ps1`)
2. Compile it into a MOF file
3. Deploy using Push or Pull mode
> DSC is particularly suitable for large-scale server configuration, compliance checks, and automated deployment.
* * *
## VIII. Command Discovery and Help System
PowerShell provides a rich help system that allows you to easily explore commands:
* `Get-Help Get-Process`: View command help
* `Get-Command`: List all commands
* `Get-Member`: View object properties and methods
* `Get-Help about_*`: View built-in conceptual documentation (e.g., `about_Execution_Policies`)
### Example: View Object Structure
Get-Process | Get-Member
* * *
## Summary
| Concept | Keyword | Brief Description | |
| --- | --- | --- | --- |
| Cmdlet | `Verb-Noun` | Smallest execution unit, based on .NET | |
| Pipeline | ` | ` | Passes objects instead of text |
| Provider | `FileSystem`, `Env:` | Unified access to various resources | |
| Module | `.psm1`, `.psd1` | Organize reusable command collections | |
| Execution Policy | `Set-ExecutionPolicy` | Control script execution permissions | |
| Remoting | `Enter-PSSession` | Execute commands remotely | |
| DSC | `Configuration` | Declarative system configuration platform | |
| Help System | `Get-Help` | Built-in documentation system |
YouTip