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!




Monday, 30 May 2016

PowerShell basis : frequently used commandlets (cmdlets)

PowerShell is scripting languages which is introduced in 2003 by Microsoft introduced.
it is an advanced automation-script language which replaces legacy DOS batch files. 

 Now a days Powershell is so famous and being used by several windows Administrators, IT Professionals, Network Admins and by different Developers of Microsoft Products.

Let's get started with some examples of using PowerShell to perform some tasks. There are around thousands of PowerShell commends used by IT, Network, SharePoint,Windows developers and Admins. To Run and execute PowerShell cmdlets, you must have appropriate permissions to execute the PowerShell commands on system.

You can load different library of C#/.net libraries (Snapin) to your session to run commands to send email, try catch exception handling logging errors, sending alert emails with attachments create interactive UI like windows forms and so many more things, by default same basis commands can be executed.

PowerShell is open source, you can run PowerShell on Linux and Mac OS X environments. Download and setup instructions are available.
 
To Start with PowerShell Yeah: lets start with me, go to strat type powershell you will get below result  that may vary based on your system version/theme








  
The first option in above screenshot is basic single command window to type and see output, second will give multi window to type see and run commands and output try both now.

Comparison Operators  in PowerShell –
  • -eq – Equals (==) to operator.
  • -lt –  Less than ( < )operator.
  • -gt – Greater than ( > )operator.
  • -le – Less than or equal to ( <= ).
  • -ge – Greater than or equal to ( >= ).
  • -like – pattern matching
lets have a basic comparison example

 $UserFirstname= "Praveen";
 if($UserFirstname -eq “Praveen”)
{
Write-Host “We are working in PowerShell ” $UserFirstName
}
else
{
Write-Host “User name is ” $UserFirstname.
}



We will look more interesting and in more details in out next part "powershell-frequently-used-commandlets-PART-2"


Some basics things you may want to know
  • Variable type need not to define.
  • Powershell cmdlets are case insensitive.
  • Commands cab be passed as input by using | (pipes) as we are using in bachfiles.
  • cmnlets can use aliases like cls is for Clear-Host, gc - Get-Content, ls - list.   
--