Sunday, 6 July 2014

Starting PowerShell from the Basic Commands !

This is the very basics of PowerShell.

You will be able to understand how to perform each and every command available in PS using help command. Before using powershell help you have to update the help with latest update. Update help is the new features available in PowerShell 3.0. Not available in previous version of PS. Update-help is the command used to update the help content. I am giving some example for updating help.

PowerShell 3.0 Updateable Help.
Below is the command to update help directly from Internet..

Update-help -force

I always recommend updating the PowerShell as it gives more detailed help as well as examples of cmdlets, so we can use the PowerShell itself as a guide for understanding the commands.

We need to run PowerShell as "Run as Administrator" (As the files will be stored under root file which is restricted by default) to perform the update

'Get-help Update-help' command provides more details about how to update help

Force is using to update the help file forcefully.

We can save the help file using save-help cmdlet. This will allows you to download the newest help files for Windows PowerShell and save in a specified directory. This feature lets you update the help file on the computer that do not have Internet availability.
Below is the cmdlet used to save the help-files and update from the saved place. We have to specify the UI Culture while downloading the help files. (Get-UICulture is the command to get the UICulture of the system)

Save-Help -UICulture en-US -DestinationPath C:\Users\vivek\PSHelp -Verbose
Update-Help -SourcePath C:\Users\vivek\PSHelp -Verbose


Can you remember all the cmdlets in powershell ???

If NO, get-command will help you to retrieve all the available powershell commands.If you remember any words in the command, you can easily retrieve the command. But it will not display the additional parameters and display all the command in console by default.

# Out-GridView : This cmdlet will display the output in  different windows and will help you to read it properly

Get-Command Get-Help
Get-command get-exe*


Get-Member is an essential command for discovering more about a PowerShell objects.As you can see, powershell output shows us some of the default properties, not all. We can use get-member cmdlet to retrieve all the properties of an object. More over we will be getting all the methods of an object by using get-member.

Get-Process | Get-Member

GM is the alias for Get-Member

A cmdlet always consists of a verb and a noun, separated with a hyphen. Some of the verbs use for you to learn PowerShell is:
  • Get — To get something
  • Start — To run something
  • Out — To output something
  • Stop — To stop something that is running
  • Set — To define something
  • New — To create something

PowerShell's execution policy 

PowerShell's execution policy is a safety feature that controls the conditions under which PowerShell loads configuration files and runs scripts. This feature helps prevent the execution of malicious scripts.
  • Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
  • AllSigned - Only scripts signed by a trusted publisher can be run.
  • RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
  • Unrestricted - No restrictions; all scripts can be run.

Saturday, 5 July 2014

Basic understandings about Windows PowerShell.

What is Powershell ???

Windows PowerShell is a task automation and object-oriented management engine based on .NET Framework which is designed especially for system administration and automate batch processing. PS is Object Oriented, but that doesn't mean you have to become a programmer to use it. PS is an extendable command shell as well.

Other PowerShell definitions are ...

PowerShell is an automation engine
PowerShell is a scripting environment
PowerShell is a command-line shell

PowerShell Versions

PowerShell v1 - November 2006. The codename for the PowerShell project was Monad ( Between 2002 and 2006 )

PowerShell 2.0 - October 2009 - This version supports remoting and enhances the capabilities of PowerShell like transactions, background jobs, events, debugging etc-:

PowerShell 3 -  September 2012 - PowerShell 3.0 doesn't support Windows XP. In this version, You can add and scheduled jobs, session connectivity, automatic module loading etc-:

PowerShell 4.0 - October 2013 - Added support for desired state configuration, enhanced debugging, network diagnostics etc-:

Is Powershell available in all the OS by default?

No. Windows Powershell is not available in all Operating systems by default. It is available from Windows 7 and it's latest and from windows-2008 R2 and its latest. We can download and install PowerShell in Windows-XP, Windows-Vista, and Windows-2003

Basic Short Notes !!! Must read.

What is a Class - In an object-oriented programming language, a class is a blueprint for an object. Classes describe the types of an object

What is an Object  - An object is an instance of a class that created dynamically.

What is a Method - A method is a code block that contains a series of statement

What is Powershell Function - Function is the way of packaging the codes that does something and return the value

What is Parameter and Argument - In general, we can think of a Parameter as a receiver of a piece of information and The argument is the information itself.

What is Enumerator - Enumerator or enum is a value data type. It is used to represent a constant set of value

What is Constructor in PowerShell - A constructor is a special method of a class which gets automatically invoked at the time of object creation.

What is an Array in PowerShell  - An array is a data structure that is designed to store a collection of items. The items can be the same type or different types - Indexes are used in arrays

What is a Hash Table - A PowerShell hash table is a data structure of key/value pairs. A key/value pair is essentially a set of two elements that are related in some manner. In its the simplest form, a hash table is just a way to store one or more sets of item names and item values.

How to Use Powershell?

Powershell engine is a set of .Net framework classes stored in a DLL file. You can not interact with it directly.

When we talk about "Using Powershell" we are most often talking about using it through a host that looks like a command-line.  Microsoft provides two host for that purpose.

Console and ISE (Integrated Scripting Environment).

Console(PowerShell.exe) is an application that runs in the command line and ISE is basically a graphical PowerShell application which is using for scripting purpose and we can have different run-space in a single window to run multiple scripts. Power GUI and SAPIEN PS Editor are the examples of some other third party host which enables more features.

Powershell and old command prompt (cmd) use the same underlying console technology, which means that you can type PowerShell in command prompt and cmd in PowerShell to switch the other shell. The exit will revert back the starting shell.

What is 64-Bit and 32-Bit Console?

This is basically pointing to PowerShell extensions. If you are loading an extension, you can only load which is built on the same architecture. The 64-Bit shell can only load the 64-Bit extension. And if you have 32-Bit extension you have to load 32-Bit shell.

Brackets in PowerShell

Parenthesis Brackets (): Curved parenthesis style brackets are used for compulsory arguments.
Braces Brackets {} : Curly brackets are employed in blocked statements
Square Brackets []: They define optional items and they are not frequently used

Why Should we learn PowerShell?

  • Believe it or not, As per my experience from the past year using the command line made my life easier. This means that I completed a task that required 6 hours in 10 Minutes by writing scripts. :) 
  • Almost all the latest products from Microsoft can be manageable by using PowerShell, like System Center Products, Active Directory, Exchange Server, SQL, Share Point Etc :
  • As we all know, there is a big competition for new IT jobs that are available. Therefore it's better if you can list Windows Powershell among your skillset.