PowerShell is an Open Source programming & scripting language from Microsoft, mainly useful for the Administrators to automate the processes related to the Applications running on the Operating Systems.
PowerShell contains commands (call them as cmdlets – command-lets). Each cmdlet is for different purpose. We can use combination of these cmdlets to perform some complex tasks. Unlike other command line tools, PowerShell allows the Users to extend the functionality by creating user-defined cmdlets.
PowerShell provides a command-line shell to allow the User to run the PowerShell commands to perform the tasks.
Type “powershell” at the command prompt, to open PowerShell shell.
C:\>powershell Windows PowerShell Copyright (C) 2013 Microsoft Corporation. All rights reserved. PS C:\>
You will see this PowerShell shell prompt, if PowerShell is already installed in your System. If not already installed, here you can get the PoweShell for Windows, “https://github.com/PowerShell/PowerShell/releases“.
PowerShell is different than other command-line tools available in the market. Below are some dominant features makes PowerShell more popular:
Features of PowerShell
PowerShell is Cross-Platform. We can use PowerShell in other Operating Systems too; like Linux, Mac OS etc.,
PowerShell Allows to Extend the Functionality with user-defined cmdlets. We can write our own cmdlets and add them into PowerShell.
As mentioned above, PowerShell is a scripting language; that means, it Allows Scripts to write and run them in PowerShell shell to run some complex tasks.
PowerShell ISE (Integrated Script Editor) is useful to write & run the scripts.
PowerShell provides hundreds of cmdlets; each provides different functionality. It is very difficult to remember all these cmdlets; keeping this in mind, PowerShell designers provides a Standard Naming Convention called as “verb-noun” convention; and cmdlets follow the same to name the commands. So it will be easy to remember the command. For example: to get the date, use “Get-Date” command. If you type “Get-Date” at PowerShell shell prompt, it will show today’s date & current time.
PS C:\> Get-Date Friday, August 09, 2018 09:05:19 AM PS C:\>
PowerShell is developed using .Net. And one great design feature of this is, it treats everything as Objects. For example: The resultant text displayed on the console when we run the commands are also internally treated as Objects and this great feature gives more control on the output.
Aliasing in PowerShell
Aliasing is another feature PowerShell allows to give alias names to the cmdlets. There are already some common names were defined as aliases and we can use the alias names to get the same functionality what cmdlet will give. For example: Get-ChildItem is the cmdlet we use to display the list of files in the current directory. It is same as the popular command “dir“. We use already defined alias name “dir” to produce the same result what “Get-ChildItem” will return. Observe that PowerShell treats below output as Objects; that means, it treats “Mode”, “LastWriteTime”, “Length” & “Name” as Objects.
PS C:\> dir Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 1/29/2015 11:50 AM AIP d---- 7/30/2018 5:36 PM GitHub d---- 4/30/2014 3:58 AM Intel d---- 11/16/2015 5:59 PM My Web Sites d---- 7/13/2018 2:25 PM New folder d---- 7/14/2009 8:50 AM PerfLogs d-r-- 8/7/2018 2:10 PM Program Files d---- 8/3/2018 1:57 PM Program Files (x86) d---- 11/14/2017 8:12 PM Temp d-r-- 7/26/2015 2:31 PM Users d---- 7/18/2018 12:52 AM Windows -a--- 7/31/2018 12:41 AM 72 a.bat -a--- 7/27/2018 1:56 PM 133954 Get-Command.txt -a--- 7/6/2016 11:43 PM 23 invalid.txt -a--- 7/27/2018 4:41 PM 11 sample.ps1
PowerShell pipeline is another interesting feature PowerShell provides to combine multiple commands to produce better results. The commands are combined using pipe “|” symbol. One command’s output is another command’s input; and the commands will execute from left to right. For example: below command sorts the results of the “dir” commands, based on the file name. Observe that the “Name” in below command we used is the return Object (as mentioned above PowerShell treats everything as Object) from “dir” command.
PS C:\> dir | Sort-Object Name Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 7/31/2018 12:41 AM 72 a.bat d---- 1/29/2015 11:50 AM AIP -a--- 7/27/2018 1:56 PM 133954 Get-Command.txt d---- 7/30/2018 5:36 PM GitHub d---- 4/30/2014 3:58 AM Intel -a--- 7/6/2016 11:43 PM 23 invalid.txt d---- 11/16/2015 5:59 PM My Web Sites d---- 7/13/2018 2:25 PM New folder d---- 7/14/2009 8:50 AM PerfLogs d-r-- 8/7/2018 2:10 PM Program Files d---- 8/3/2018 1:57 PM Program Files (x86) -a--- 7/27/2018 4:41 PM 11 sample.ps1 d---- 11/14/2017 8:12 PM Temp d-r-- 7/26/2015 2:31 PM Users d---- 7/18/2018 12:52 AM Windows PS C:\>
MicroSoft designed PowerShell with consistency, provides a framework with basic features; due to this Consistent Design the built-in commands & user-defined commands follow the same standards when implementing cmdlets. This allows to the Developers to focus mainly on the command’s functionality. For example: The Developers of new cmdlet, no need to focus on the Sorting functionality; it is already provided by PowerShell through “
Sort-Object” cmdlet. Above example describes this.
We will discuss more about PowerShell with working examples, in upcoming Articles.
Don’t forget to give the feedback, through below Comments section.