PowerShell Cmdlets and Alias

In the previous post, we covered variables in PowerShell. In this post, we will be discussing cmdlets. It is pronounced as "command-lets." It is the fundamental building block of PowerShell. It is a small, single-purpose, lightweight command that performs a specific task. It is designed to be used in combination with other cmdlets to accomplish complex tasks.

PowerShell includes hundreds of built-in cmdlets for tasks such as managing the file system, managing services, and managing processes. Additionally, many third-party software vendors provide PowerShell cmdlets for their products, including Microsoft SQL Server.

Cmdlets have a consistent naming convention of Verb-Noun, where the verb describes the action to be taken, and the noun describes the object the action is being taken on. For example, the cmdlet for creating a new folder is New-Item, where "New" is the verb, and "Item" is the noun.

To use a cmdlet, simply type its name followed by any required parameters. Let's take a look at an example using the Get-ChildItem cmdlet:

Get-ChildItem -Path C:\Rajanand\PowerShell\

In this example, the Get-ChildItem cmdlet is used to retrieve a list of all items in the C:\Rajanand\PowerShell\ folder.

Cmdlets can also be piped together to perform more complex tasks. For example, let's say we wanted to find all files in the folder whose file name starts with data. We could use the Get-ChildItem and Where-Object cmdlets together like this:

Get-ChildItem -Path C:\Users\Rajanand | Where-Object {$_.Name -like "data*"}

In this example, we first use the Get-ChildItem cmdlet to retrieve a list of all items in the C:\Rajanand\PowerShell\ folder. We then pipe(i.e., |) that list to the Where-Object cmdlet, which filters the list to only include items with a Name property starts with data.

Finally, we pipe the filtered list to the Select-Object cmdlet, which selects and displays only the Name, CreationTime, LastWriteTime and Length properties of the remaining items.

Alias

It's worth noting that many cmdlets also have aliases, which are shorter or alternative names for the same cmdlet. For example, the Get-ChildItem cmdlet has an alias of ls or dir . ls is used in Unix based system to list out the files in the current directory and dir is used in windows for the same purpose.

As you can see below, We are able to get the list of items in a directory using ls and dir alias of Get-ChildItem

If you want to know about all the alias defined, you can get the same using the cmdlets Get-Alias

Similary, If you want to set an alias for any of the cmdlets that you use frequently, then you can achieve the same using Set-Alias

In conclusion, cmdlets are the basic building blocks of PowerShell and provide a powerful and flexible way to perform a wide range of tasks. Alias is used to simplify the command that we frequently use.

Did you find this article valuable?

Support Rajanand Ilangovan by becoming a sponsor. Any amount is appreciated!