PowerShell Cmdlet Basics

Cmdlet (pronounced similar to "command-let")is the basic command unit in PowerShell, a small command implemented by Microsoft based on the .NET framework.

Cmdlets are different from external programs in traditional shells, such as.exeor.batfiles; instead, they are built into the PowerShell runtime environment.

Each Cmdlet performs a specific task, such as retrieving data, setting properties, creating objects, exporting files, and so on.

Cmdlet naming convention: Verb-Noun

All Cmdlets in PowerShell follow a unified"Verb-Noun"format, for example:

  • Get-Process: Gets process information
  • Set-Date: Sets the system date
  • New-Item: Creates a new item (such as a file or folder)
  • Remove-Service: Deletes a service

This naming convention is intuitive and consistent, making it easy to remember and search.


Basic syntax structure of Cmdlets

The syntax structure of a Cmdlet is generally as follows:

Verb-Noun [-ParameterName ParameterValue] [-SwitchParameter]

Example:

Get-Service -Name W32Time

Explanation:

  • Get-Service: Gets the service object
  • -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, no value needs to be specified

Combining parameters with pipelines

Cmdlets supportpositional parameters、named parametersandpipeline 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 the pipeline

"notepad" | Get-Process -Name

Example 3: Piping objects to another Cmdlet

Get-Process notepad | Stop-Process

In the above example,Get-Processgets the notepad process object, then passes it through the pipeline toStop-Processto terminate it.


Viewing Cmdlet help information

PowerShell provides a complete help system; you can useGet-Helpto view the usage of any Cmdlet:

Get-Help Get-Process

To see more parameter descriptions and examples, add-Detailedor-Examples:

Get-Help Get-Process -Examples

If you are using PowerShell for the first time, it is recommended to run the following command once to update the local help:

Update-Help

Common basic Cmdlet quick reference table

Cmdlet Function description
Get-Command View all available commands
Get-Help View command help information
Get-Process Get process list
Get-Service Get service list
Start-Service Start service
Stop-Service Stop 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 tocls

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 suggestions

  • Cmdlets are the core units of PowerShell; each Cmdlet is a task executor with a clearly defined function.
  • The unified "Verb-Noun" naming convention makes Cmdlets predictable and easy to learn.
  • The combination of the pipeline, parameter system, and object model makes Cmdlets excel in data processing and automation.
  • Mastering common Cmdlets and combining them with object operations is an important starting point for learning PowerShell.

Beginners are advised to practice from the following aspects:

  1. UseGet-Commandto explore all available commands
  2. combined withGet-Helplearn to look up command usage
  3. UtilizeNew-ItemandGet-Contentand other commands for local file operations
  4. Try more often to combine commands through pipelines to process data
Other extensions