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

PowerShell Cmdlet Basics

PowerShell Cmdlet Basics

Cmdlet (pronounced like 'command-let') is the basic command unit in PowerShell, a small command implemented by Microsoft based on the .NET Framework.

A Cmdlet differs from external programs in a traditional shell, such as .exe or .bat files; instead it is built into the PowerShell runtime environment.

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

Cmdlet naming convention: verb-noun

All Cmdlet names in PowerShell follow a unified 'verb-noun' format; for example:

This naming approach is both intuitive and consistent, making it easy to remember and find.


Basic syntax structure of a Cmdlet

The syntax structure of a Cmdlet is generally as follows:

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

Example:

Get-Service -Name W32Time

Explanation:

  • Get-Service: Get a service object
  • -Name W32Time: Specify the service name to query as W32Time

Another example:

Stop-Process -Id 1234 -Force

  • -Id is a parameter that takes a value
  • -Force is a switch parameter that does not require a value

Combining parameters with the pipeline

Cmdlets supportpositional parameters, named parametersandpipeline input. This allows commands to be flexibly combined to build complex workflows.

Example 1: Specifying parameters explicitly

Get-Process -Name notepad

Example 2: Passing through the pipeline

"notepad" | Get-Process -Name

Example 3: Passing an object through the pipeline to another Cmdlet

Get-Process notepad | Stop-Process

In the example above,Get-Process retrieved the notepad process object, then passed it through the pipeline to Stop-Process to terminate it.


View 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 see 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 run the following command once to update the local help:

Update-Help


Common Basic Cmdlet Cheat Sheet

Cmdlet Description
Get-Command View all available commands
Get-Help View help information for a command
Get-Process Get the process list
Get-Service Get the service list
Start-Service Start the service
Stop-Service Stop a service
Set-ExecutionPolicy Set the execution policy
New-Item Create a new file or folder
Remove-Item Delete a file or folder
Copy-Item Copy a file or folder
Move-Item Move a file or folder
Clear-Host Clear the 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 Suggestions

  • A Cmdlet is the core unit of PowerShell; each Cmdlet is a task executor with a clear function.
  • The unified 'verb-noun' naming convention makes Cmdlets predictable and easy to learn.
  • The combination of the pipeline, parameter system, and object model enables Cmdlets to excel at data processing and automation.
  • Mastering common Cmdlets and combining them with object operations is an important starting point for learning PowerShell.

It is recommended that beginners practice in the following areas:

  1. Use Get-Command Explore all available commands
  2. together with Get-Help Learn how to look up command usage
  3. Use New-Item and Get-Content and other commands for local file operations
  4. Try more often to combine commands through the pipeline to process data