Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PowerShell Core Concepts

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:

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)

Modules (.psm1 / .psd1)

A module is a functional unit for reuse in PowerShell; it can be:

A module can support:

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:

Usage

  1. Write a configuration file (.ps1)
  2. compile it into a MOF file
  3. 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:

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