Cmdlet, Alias, Function and Modules in Windows Powershell
Cmdlets in Windows Powershell
Cmdlets are a .NET classes. Each Cmdlet is in the form “verb-noun”. For example to get he list of processes, you can use “Get-Process” Cmdlet. Each Cmdlet has methods like BeginProcessing(), ProcessRecord() and EndProcessing(). Cmdlets get data from providers like file system, registry. Modules are used when you want to reuse the code.
You can get the members of object returned by specific Cmdlet using below syntax.
Get-Process | Get-Member
Alias in Windows Powershell
Alias is nothing but another name for the Cmdlet. For example – You must be familiar with “ls” command in Linux. This command displays the files and directories in current directory.
In Windows Powershell, we have “Get-ChildItem” Cmdlet to do the same thing. But cool thing is that in Powershell, we can use “ls” command without any problems. This is possible due to Alias.
To list down all alias in Powershell, you can use below command.
To create new Alias, you can use below command.
> Set-Alias -Name myls -Value get-childitem
To export an alias from current session to file, you can use below command.
> Export-Alias -Path “myalias.csv”
To import an alias from file, you can use below command. If any alias already exists, error will be thrown.
> Import-Alias myalias.csv
You can also find out main command running behind alias using below command.
Get-Alias –Name ls
Output of above command will be Get-ChildItem.
When you view help of any command, it shows the aliases for that command there.
Here is the list of some of the popular Linux aliases in Windows Powershell.
- ls – Get-ChildItem, gci, dir
- cat – Get-Content, gc, type
- man – Get-Help, help
- clear – Clear-Host, cls
- cp – Copy-Item, cpi, copy
- mv – Move-Item, mi, move
- rm – Remove-Item, ri, del, erase, rmdir, rd
- mv – Rename-Item, rni, ren
- pwd – Get-Location, gl, cd
- popd – Pop-Location
- pushd – Push-Location
- cd – Set-Location, sl, chdir
- echo – Write-Output, write
- ps – Get-Process, gps
- kill – Stop-Process, spps
- curl – Invoke-WebRequest, iwr, wget