Cmdlet (pronounced like "command-let") is the basic command unit in PowerShell, small commands implemented by Microsoft based on the .NET framework.
Cmdlets differ from external programs in traditional shells, such as .exe or .bat files, in that they are built into the PowerShell runtime environment.
Each Cmdlet performs a specific task, such as retrieving data, setting properties, creating objects, exporting files, etc.
Cmdlet Naming Convention: Verb-Noun
All Cmdlets in PowerShell follow a unified "verb-noun" format, for example:
Get-Process: Retrieves process informationSet-Date: Sets the system dateNew-Item: Creates a new item (such as a file or folder)Remove-Service: Removes a service
This naming convention is intuitive and consistent, making it easy to remember and find.
Basic Cmdlet Syntax Structure
The syntax structure of a Cmdlet is generally as follows:
Verb-Noun
Example:
Get-Service -Name W32Time
Explanation:
Get-Service: Retrieves service objects-Name W32Time: Specifies the service name to query asW32Time
Another example:
Stop-Process -Id 1234 -Force
-Idis a parameter that takes a value-Forceis a switch parameter, which does not require a value to be specified
Combining Parameters with Pipelines
Cmdlets support positional parameters, named parameters, and pipeline input. This allows commands to be flexibly combined to build complex workflows.
Example 1: Specifying parameter form
Get-Process -Name notepad
Example 2: Passing through pipeline
"notepad" | Get-Process -Name
Example 3: Object pipeline passed to another Cmdlet
Get-Process notepad | Stop-Process
In the above example, Get-Process retrieves the notepad process object, then passes it through the pipeline to Stop-Process to terminate it.
Viewing Cmdlet Help Information
PowerShell provides a complete help system. You can use Get-Help to view the usage of any Cmdlet:
Get-Help Get-Process
To view more parameter descriptions and examples, add -Detailed or -Examples:
Get-Help Get-Process -Examples
If you are using PowerShell for the first time, it is recommended to execute the following command once to update the local help:
Update-Help
Common Basic Cmdlet Quick Reference Table
| Cmdlet | Description |
|---|---|
Get-Command |
View all available commands |
Get-Help |
View help information for commands |
Get-Process |
Get process list |
Get-Service |
Get service list |
Start-Service |
Start a service |
Stop-Service |
Stop a service |
Set-ExecutionPolicy |
Set execution policy |
New-Item |
Create new file or folder |
Remove-Item |
Delete file or folder |
Copy-Item |
Copy file or folder |
Move-Item |
Move file or folder |
Clear-Host |
Clear screen, similar to cls |
Practical Example: File Operations
Create a folder:
New-Item -Path "C:\TestFolder" -ItemType Directory
Create a text file in that directory:
New-Item -Path "C:\TestFolder\demo.txt" -ItemType File
Write content to the file:
Set-Content -Path "C:\TestFolder\demo.txt" -Value "Hello PowerShell"
Read file content:
Get-Content -Path "C:\TestFolder\demo.txt"
Summary and Learning Recommendations
- Cmdlet is the core unit of PowerShell, with each Cmdlet being a task executor with clear functionality.
- The unified "verb-noun" naming convention makes Cmdlets predictable and easy to learn.
- The combination of pipelines, parameter systems, and object models makes Cmdlets excellent for data processing and automation.
- Mastering common Cmdlets and combining them with object operations is an important starting point for learning PowerShell.
Beginners are recommended to practice from the following aspects:
- Use
Get-Commandto explore all available commands - Use
Get-Helpto learn how to look up command usage - Use commands like
New-ItemandGet-Contentfor local file operations - Try combining commands through pipelines to process data
YouTip