Powershell For Beginners : Introduction to Windows powershell
Powershell is a scripting and automation platform for Miscrosoft platform which is built on top of .Net framework. it is useful for system tasks automation, system administration etc. on windows systems. Powershell is basically a replacement of Command Prompt (cmd.exe) on windows based systems.
Powershell borrowed a lot of functionality and commands from the Linux environment including many Linux commands. For example the ability to pipe commands and link commands into a script.
The cmdlets or commandlets are single commands that accomplish (sometimes) more complex tasks similer to functions. cmdlets follow
verb-noun pattern, for example “get-help“. This help users understand the purpose of the commands. Verbs include New, Set, Get, Add and Copy. Microsoft has a documented list of approved verbs and their intended uses to help maintain consistency throughout the platform. When placed together with nouns, you get cmdlets such as:
Opening Powershell window : Goto search bar and type powershell and then you have powershell interpreter.
Now lets see some examples :
- Get-Help : Get-help cmdlets shows the help page about a command or cmdlet. It is basically works like man command of linux terminal.
The write-Output command is similar to
echo command. It has also alias for echo, write.
- Get-Process : Lists all the processes.
Some Resources for Cmdlets :
PowerShell Integrated Scripting Environment :
Windows (7, 8, 10..) has an integrated IDE pre-installed called Powershell ISE to write and run Powershell script. It also has a help tab which lists all the cmdlets with details. To open Powershell ISE type “Powershell ISE” on search bar and open the app.
The example use of powershell ISE
Running a Powershell script :
To run a powershell script right click on script and click “Run with Powershell”.
Some Common Commands/Cmdlets used in Powershell :
|Get-Location||pwd,gl||shows current working directory|
|Get-Help||help,man||Show help page of commands|
|get-Content||cat,type,gc||Get the content of a file|
|Get-Command||gcm||List all the commands|
|Get-Childitem||ls,dir,gci||list all contents in directory|
|Push-Location||pushd||Push current directory location on stack|
|Pop-Location||popd||Change directory to currently pushed location|
|Tee-Object||tee||Pipe standard input into file and standard output stream|
|Write-Object||echo,write||print string, variables to standard output|
|Get-Process||ps,gps||list all the processes|
|Stop-Process||kill,spps||stop a running process|
|Select-String||sls||Print line matching a pattern(similar to grep)|
|Set-Variable||sv,set||Set the value of a variable/create a variable|
|Invoke-WebRequest||iwr,curl,wget||Gets content from web page on the internet|
Now in next blogposts we are going to look at the basic powershell scripting concepts and methods.