Sunday 12 June 2016

PowerShell Turorial (including basic commandlets) part 2


PowerShell tutorial for beginners with basics Part2 will cover some basics of commands and how to create and run PowerShell scripts, as we have seen in first part “Powershell basis” why and who all can use PowerShell so let’s start-
Go to start option start typing PowerShell you will able to see at least two option in your windows system, as it is already installed for other OS please refer blog part1 for details


Option first will open the Console screen, like command prompt as you may have used to run commands or batch files earlier, and option two in screenshot will open PowerShell scripting environment with option to search commands and write commands with intelliSense and see output at the same time, this is really cool, we cover this in our next blog, I have shortlisted some basic commands and let’s run some examples,

Get Help

PowerShell has over a hundreds of commands and introduced so many new commands in recent version, mean there is a lot to learn interesting things to make work easy, but don’t worry Microsoft has provided comprehensive help system that can help you find commands to accomplish tasks, and it explains how to use those commands.

The command for getting help is get-help and it accepts a variety of different arguments. For example, to display the help file entry for the get-help command, type:

get-help get-help
And then press Enter.

Get-help also accepts a keyword for searching the documentation, and returns a list of results containing that keyword. To export any objects values to the csv (coma separated values, an excel file) you can run:

get-help epCSV

Export-CSV – it exports file output into csv file, we will see examples in next part of the blog

Help file entries are displayed directly in the console. However, by using the parameter “online” you can open a web browser and view the help on Microsoft’s TechNet website:

get-help Get-Host -online
I found it useful but it’s comparative slow to get result.

How you would create and run a PowerShell script (many series of commands or a program file)?



PowerShell file can be created with .ps1extension only. I have created a file Fisrt.ps1 for demo, here we will see how can we run and create script.

Edit your file and add the following line:






Save the file and return to the PowerShell window. In order to run the script, the most common method is by calling it:
& "C:\PS\First.ps1"
Or you can try to goto your folder and type file name First.ps1 and hit enter

Just go ahead and try to do that while reading. You may get an error with red text saying scripts have been disabled on your system.

This is normal behavior. In order to prevent malicious scripts from running on your system, PowerShell enforces an execution policy. There are 4 execution policies you can use:

·         Restricted – Scripts won’t run. Period. (Default setting)

·         RemoteSigned – Locally-created scripts will run. Scripts that were created on another machine will not run unless they are signed by a trusted publisher.

·         AllSigned – Scripts will only run if signed by a trusted publisher (including locally-created scripts).

·         Unrestricted – All scripts will run regardless of who created them and whether or not they are signed.

In order to use our newly-created script, we will have to modify our execution policy to allow our script to run. Since we have not digitally signed our new script, our options for setting the execution policy are left to “RemoteSigned” and “Unrestricted.” We are going to change it to RemoteSigned.

In order to change the execution policy, we will need to reopen PowerShell as an Administrator (the command will fail otherwise) and run the following command:
Set-ExecutionPolicy RemoteSigned
It will ask to verify that you really want to change the execution policy. Go ahead and select Y for yes, then go ahead and close and reopen your Powershell window.

After restarting the Powershell window, go ahead and try running that script again

& "C:\PS\First.ps1"

Wow, good! You have completed your first script!