Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 34 ยท Getting Started

Cmdlets and the Verb-Noun Pattern

Understand PowerShell cmdlets: the approved Verb-Noun naming, Get-Command discovery, named and positional parameters, switch parameters and aliases.

A cmdlet (say "command-let") is the basic building block of PowerShell: a small command that does one job and returns objects. Get-Process lists processes, Stop-Service stops a service, Set-Content writes to a file. The names follow a strict Verb-Noun pattern. Once you know the pattern you can guess commands you have never seen, and find them with a search instead of a web page.

This lesson covers the naming rules, how to discover commands, how arguments are matched to parameters (named, positional and switch), and what aliases are and when to avoid them. The real commands are read-only. A JavaScript stand-in at the end shows how a command line is split into a command and its parameters. If you have not yet met the shell, start with Installing and Your First Commands.

The Verb-Noun pattern

Every cmdlet name has two parts joined by a hyphen: an action and a thing.

Cmdlet Verb Noun
Get-Process Get Process
Stop-Service Stop Service
New-Item New Item
Remove-Item Remove Item
Set-Content Set Content

The verbs come from an approved list, so the same action always has the same word. You find items with Get-, change values with Set-, create with New-, delete with Remove-. There is no Delete-Item or Fetch-Item; module authors are expected to use the standard verbs. You can see the whole list yourself:

POWERSHELL
Get-Verb
Get-Verb -Group Common | Sort-Object Verb

Get-Verb also reports a group (Common, Data, Lifecycle, Diagnostic, Communications, Security, Other) so you can pick the right verb when you write your own commands later.

Verbs say what happens

Get Read something, change nothing.
Set Change an existing thing.
New Create something.
Remove Delete something.

Same verbs across every module.

1 / 2

A useful consequence: if you know the noun, you can predict which verbs exist. Commands that read are safe to experiment with, while Remove-, Stop- and Set- change things.

Finding commands with Get-Command

Get-Command searches every command available in your session: cmdlets, functions, aliases and external programs.

POWERSHELL
Get-Command                          # everything
Get-Command -Verb Get                # all Get- commands
Get-Command -Noun Process            # every command that works on processes
Get-Command *service*                # wildcard on the name
Get-Command -Module Microsoft.PowerShell.Management

Searching by noun is the fastest way to discover what you can do with a topic. Get-Command -Noun Service shows Get-Service, Start-Service, Stop-Service, Restart-Service, Set-Service and New-Service on Windows, a nice demonstration of the pattern. Which commands you see depends on your operating system and loaded modules.

PowerShell loads modules on demand. A command from an installed but not yet loaded module still appears in Get-Command and loads automatically when you run it.

Parameters

Cmdlets are configured with parameters, written with a leading dash. Parameter names and cmdlet names are not case sensitive.

POWERSHELL
Get-ChildItem -Path C:\Windows -Filter *.log -Recurse

There are three kinds you will use constantly.

Named parameters give the name, then the value: -Path C:\Windows.

Positional parameters let you drop the name when the value is in the expected position. For Get-ChildItem, -Path is position 0, so these are identical:

POWERSHELL
Get-ChildItem -Path C:\Windows
Get-ChildItem C:\Windows

Which parameters are positional, and in what order, is shown in the help (see Getting Help). In scripts, using names makes the code easier to read.

Switch parameters take no value: their presence means "on". -Recurse is a switch. You can also set one explicitly with a colon: -Recurse:$false.

PowerShell also accepts the shortest unambiguous prefix of a parameter name, so -Rec works for -Recurse. That is handy at the prompt and a poor habit in scripts, because a future version of the command could add a second parameter starting with the same letters.

Common parameters

Almost every cmdlet also accepts a set of common parameters: -Verbose, -Debug, -ErrorAction, -WarningAction, -OutVariable and more. Cmdlets that change things also support -WhatIf (show what would happen) and -Confirm (ask first). For example, Remove-Item old.log -WhatIf prints what would be deleted and deletes nothing. You will use these a lot in Advanced Functions.

Aliases

An alias is a short nickname for a command. PowerShell ships with many:

Alias Command
ls, dir, gci Get-ChildItem
cd, sl Set-Location
cat, gc Get-Content
? Where-Object
% ForEach-Object
gm Get-Member
POWERSHELL
Get-Alias ls                       # which command does ls point to?
Get-Alias -Definition Get-ChildItem   # which aliases point to this command?

Aliases are fine for typing at the prompt. In scripts, shared code and teaching material, write the full name. Aliases can differ between computers (and between platforms) and they make code harder for readers to understand.

Try it: split a command line

A real command line is parsed by PowerShell's parser into a command name, named parameters, switches and positional values. This stand-in implements a much simpler version in JavaScript, so you can see the binding idea. The tiny "definition" table below plays the role of the command's metadata.

JS Stand-in: binding named, positional and switch parameters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the three ideas (named values, positional slots, switch flags) and the case-insensitive matching. What is the stand-in: the real parser also handles quoting, expressions in parentheses, parameter sets, prefix matching and type conversion.

Verbs and nouns as a lookup table

Another way to see the pattern is as a grid. This stand-in builds a list of cmdlet names from verbs and nouns and shows how you could search it the way Get-Command -Verb and -Noun do. The set of names is a small sample, not the real list.

JS Stand-in: searching commands by verb or noun

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Not everything is a cmdlet

When you type a name, PowerShell checks, roughly in this order, for an alias, a function, a cmdlet and then an external program on your PATH such as git or ping. A cmdlet is compiled .NET code; a function is PowerShell code; both look the same when you use them. Get-Command shows the CommandType of each, which is useful when two things share a name.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which cmdlet name follows PowerShell's naming convention?

    Names are Verb-Noun with an approved verb. Remove is approved; Delete is not. Get-Verb lists the approved verbs.
  2. 2. How do you list every command that works with services?

    Get-Command can filter by noun. Get-Verb lists verbs and Get-Alias lists nicknames.
  3. 3. What is -Recurse in Get-ChildItem -Recurse?

    A switch is on when present and off when absent. You can also write -Recurse:$false to set it explicitly.
  4. 4. Why should scripts avoid aliases such as gci and %?

    Full names are self-describing and portable. Aliases are a convenience for typing at the prompt.

Summary

  • A cmdlet is a small command named Verb-Noun, using an approved verb from Get-Verb.
  • Get-Command -Verb, -Noun and wildcards help you discover commands.
  • Parameters can be named, positional or switches; names are not case sensitive.
  • Common parameters such as -Verbose, -ErrorAction, -WhatIf and -Confirm work on most cmdlets.
  • Aliases like ls, cd and % are for typing; write full names in scripts.

Next: Getting Help.