Updated March 24, 2023
Introduction to cmdlets in PowerShell
Cmdlets are specialized commands in the PowerShell which implement various functions to perform specific tasks. These functions are native functions and perform various tasks. A cmdlet is a lightweight command that is used in the Windows PowerShell environment. At the time of writing any automation scripts, these commands are useful. I will tell you some examples, suppose you have some files you wanted to calculate file size, you can use PowerShell cmdlets. You want to read the file and its behaviors you can use cmdlets.
Explanation of cmdlets in PowerShell
There are many cmdlets, we will discuss their uses and examples with the real world. To write a cmdlet, we must and should use a cmdlet class that derives from one of two specialized cmdlet base classes. They must be in the below format.
- An attribute declaration is needed, and it should identify the derived class as a cmdlet.
- Define public properties that are decorated with attributes that identify the public properties as cmdlet parameters.
- We need to Override at least one or more than one input processing method.
Before going to discuss we should know a general syntax of it,
Syntax:
Object/file/string /numer/etc Cmdlets-name1 | Cmdlets-name1 | Cmdlets-name1
Cmdlets-name1 ,Cmdlets-name2, Cmdlets-name3 are the different cmdlets which we can use according to our requirements.
Different cmdlets in PowerShell
Let us discuss cmdlets,
1. Get-Unique
Suppose we have an array with various users and we want unique users from the list then we will use Get-unique. An example of it along with the screen is given below.
$user=”Ranjan”,”Ajay”,”Vikash”,”Akash”,”Vijay”,”Ranjan”,”Ajay”,”Akash”
$user
Output:
Here the Same user is getting repeated twice.
$user=”Ranjan”,”Ajay”,”Vikash”,”Akash”,”Vijay”,”Ranjan”,”Ajay”,”Akash”
$user | get-unique
Output:
So from the above example, we can see a small command can change the whole output.
2. Sort
Sorting is one of the most important expectations of any programing language, many times we can have lakhs of records and we would like to see data in a particular order, Suppose we have some users but we want to the user in alphabetical order then we will use sort, we can use get-unique to get unique results.
$user =”Ranjan”,”Ajay”,”Vikash”,”Akash”,”Vijay”,”Ranjan”,”Ajay”,”Akash”
$user | sort | get-unique
Output:
3. Measure-Object
Suppose we have one file and we want to know the numbers of lines, words, and characters on the file then we will use Measure-Object cmdlet, Measure-Object cmdlet can be used to get the properties of the passed output such as min, max, size, count, line, etc. These examples, we’re seeing the Measure-Object cmdlet in action.
Create a file sss.txt in desktop and write two-line first line “hello friend” and second line “hello friend how are u” and execute the below command:
get-content /home/ranjan/Desktop/sss.txt | measure-object
Remember you must create a file with sss.txt to run this command else it will throw an error.
Output:
4. Compare-Object
If you have two files and both files contain few lines with the help of Compare-Object command we can check similar lines or match lines from both the file, So in a very simple word Compare-Object cmdlet can be used to compare two objects. In the below examples, we’re seeing the Compare-Object cmdlet in action.
In this example, first, we have a file sss.txt in /home/ranjan/Desktop with content with first-line “hello friend” 2nd line “hello friend how are u” and in 3rd line “hello friend”. We created another file with the name sss1.txt /home/ranjan/Desktop with content “hello friend”. We are going to compare lines and matches will be displayed.
Compare-Object -ReferenceObject $(Get-Content /home/ranjan/Desktop/sss.txt) -DifferenceObject $(Get-Content /home/ranjan/Desktop/sss1.txt)
Output:
5. Sort-Object
The cmdlet is used to sort objects by its properties. In these examples, we see the Sort-Object cmdlet uses with example.
$name=”Ranjan”,”Ajay”,”Vijay”,”Sujit”,”Ajeet”
$name | Sort-Object
Output:
6. Read-Host
cmdlet this command allows us to read the value from the input string. In these examples, we see the Read-Host cmdlet in action.
$city = Read-Host "Select your city please"
$city
Output:
Here we can see it asked to enter your city name and your enter value becomes your city name variable $city value become “Dhanbad”.
7. Start-Sleep
Suppose you want to suspend session for a few seconds then we can use “Start-Sleep”
Code:
Start-Sleep 15
Output:
Your session will stop for 15 seconds. In these examples, we see the Start-Sleep cmdlet in action.
8. Invoke-History
At the time of work suppose you want to see history activity, forgetting the last command then we can use Invoke-History command. An example is given below,
Invoke-History
Output:
9. Write-Warning
We can customize warning messages, below are some examples,
Write-Warning "process taking more time"
Output:
10. ForEach-Object
This is a very frequently used command, suppose you have some array of students’ marks and we want to add 10 marks in each student’s marks. So for this, we have a cmdlet called ForEach-Object, an example is given below along with the screen.
$marks =1000,1002,1004,1005
$marks | ForEach-Object -Process {$_+10}
Output:
11. Get-History
This is different than Invoke-History, as in this case, we will see all activity, or list of all activity, an example of it along with the screen is given below.
In these examples, we see the Get-History cmdlet in action.
Get-history
Output:
The get-culture cmdlet is used to get the current culture set in windows. In these examples, we see the Get-Culture cmdlet in action.
get-culture
Output:
12. Invoke-Expression
We can assign an expression to a variable and can invoke that variable with its expression value, in the example below we are assigning a “Get-Process” command to variable $cmd and then invoking it with Invoke-Expression $cmd.
$cmd = ‘Get-Process'
$cmd
Invoke-Expression $cmd
Output:
Conclusion
To conclude, We have seen various commands of PowerShell, these commands are very useful in writing an automation script, with the help of these commands we can manipulate and work on any time of data and situations.
Recommended Articles
This is a guide to cmdlets in PowerShell. Here we discuss the explanation and top 12 different cmdlets of PowerShell in detail. You may also look at the following articles to learn more –