PowerShell Basics: CmdLets
Everything you need to know (Syntax, Examples)
What We'll Learn:
In this short guide we’re going to learn the very basics of PowerShell.
- We’ll start, by learning exactly what cmdlets are.
- We will then learn how cmdlets differ from regular Unix or Cmd commands.
- And finally, we’re going to get familiar with them by taking a look at a few essential examples.
Let’s get started!
What Are Cmdlets ( PowerShell Commands)?
Since PowerShell is a text-based user interface, the only way to interact with it is to type commands, these commands are called cmdlets (pronounced command lets).
Cmdlets are basically special words that are made to perform a designated task, everything from displaying specific information or changing windows settings.
What makes them special is that, since PowerShell is essentially a programming language, cmdlets are objects and not just executable programs.
What is a PowerShell object?
We previously mentioned that cmdlets are objects. But what exactly are objects?
An object in programming is an abstract data type, they can include multiple properties and methods or even contain other objects within them.
Objects are also commonly referred to as classes and if you are a programmer you are probably familiar with them.
If not, simply think of an object as a box that contains both information about an item as well as a set of actions that can be taken on that item.
PowerShell Cmdlets vs Commands
The best way to understand cmdlets is by comparing them to something you might already be familiar with such as regular commands in a Windows or Unix environment.
While both look similar, PowerShell cmdlets are fundamentally different from traditional commands. Main differences are as follows:
- Since PowerShell is fundamentally a programming language, cmdlets are .NET class objects and not just standalone executables.
- Cmdlets process objects from a pipeline and not text inputs. In the same way, objects are sent as output instead of plain text.
- They can be created from only a few lines of code and process only one object at a time.
While these are not all their differences, they are the ones I believe are the most important.
Now that we have a general idea of what cmdlets are lets start using them.
A PowerShell cmdlet always consists of a verb and a noun, separated by a hyphen, which makes them self-descriptive and easy to understand.
The way it functions, depends on its verb and noun.
As an example, Get-Item which consists of the Get verb along with the Item noun, is used to retrieve information about an item, while New-item is used to create an item. Most common verbs are New, Set, Get, Stop, and Start.
To better understand this concept, let’s take a look at an example.
Let’s use Get-ChildItem to view every file and folder in our current directory.
Every file and folder in your current directory will be displayed immediately.
While we have already showed a few examples so far, lets have a look at a few more, so that we can better understand cmdlets.
While I cannot show you every cmdlet there is, we are going to take a look at the ones that are commonly used.
We will be categorizing them based on their verbs.
Like we said before, a cmdlet behaves accordingly to its verb and noun, that is to say, cmdlets that share the same verb perform an action in a way that is somehow similar. With that said lets start with:
Cmdlets that share the Get verb are used to retrieve items (objects). These items can be anything from the contents of a text file to which processes are being executed on a computer.
For example using get-process, we can view every process in our computer.
Cmdlets that use the set verb are used to modify (set) some sort of value. This value could be anything from a setting or option in windows to the contents of a text file.
Cmdlets with the new verb create some sort of item. This could be anything from a new file or folder to a firewall rule, a scheduled task or even a local user.
If you want to create a new computer user for example, use new-localuser
Stop and Start Cmdlets:
Finally we have cmdlets that use the Stop and start verbs. These can be used to start or stop a service, process or some other function accordingly.
For example we can start an applications using Start-Process along with the name of the application.
Similarly using Stop process we can terminate the execution of our application.
- Cmdlets are basically special words that are made to perform a designated task.
- A PowerShell cmdlet always consists of a verb and a noun, separated with a hyphen, which makes them self-descriptive and easy to understand.
- An object in programming is an abstract data type, they can include multiple properties and methods or even contain other objects within them.
You now know what cmdlets are and how they can be used.
If you liked this short guide take a look at a few of our other posts related to PowerShell.
If you really liked it consider enrolling in our windows PowerShell course where you will learn the ins and outs of PowerShell.