Thursday, April 26, 2012

These days i am very busy in some other projects so that's why this months i post only few articles. Sorry for that.

Does anytime you think to save all the commands and there output in to text file? it like storing all of the PowerShell Consoles session data.

If yes and you don't know how to do it , let me explain it.

it is very simple. we just need to use two commands these are: Start-Transcript , Stop-Transcript

Let see how to use it.

Open you Powershell console , and before running or doing anything else , type Start-Transcript and the path to txt file in which you want to save all the output.

Start-Transcript d:\Log.txt

You can notice in below screenshot it is showing that Transcript started and it the output file is D:\Log.txt.

Now lets run Few Cmdlets.

Dir

Get-Process

one wrong command for testing error

All done now, type Stop-Transcript, cmdlet to stop recording the session. Transcript is stopped and now lets open the Log.txt file and see if that store all the things which we have done in our PowerShell console session.

You can see in below screenshot that everything which we have done is saved in to a text file. :)

Thursday, April 12, 2012

As i mentioned in the previous article that in the "PowerShell Basics" series we are going to cover the basics of PowerShell. Today we are covering variables. Variables plays an very important role in every scripting language and same in Powershell.

What are variables.?

Variables are just a container which can store anything {data} in it. We can Store "Strings", Integers, Doubles, or Output of the commands.

How to create a variables.?

It very simple, we can use New-Variable Cmdlet to create a new variable, Use -Name to named the variable and -Value to assign a value to it.

New-Variable -Name "Name" -Value "Aman"

We just created a new variable ,but

How we can see or access variables ?

use dollar sign $ with variable name to access it.

$name

There is another and easy way for creating the variables.

Just provide the name which you want to use preceding with $ sign and then type = equal to {assignment operator} and after that value.

$name = "aman"

now try to access it using $name

Variables and Strings

Adding String in Variables

To add strings in variable make sure that the desired string is wrapped in single or double quotes.

If you don't wrap in quotes you will get the below error.

Integer and Variables

Saving integer in a variable is simple. Just assign the value to variable .

$var = 987

Storing Output of Cmdlets

we can store the output of Cmdlets in to the variables. You just need to specify the command which's output you want to store in a variable. I in below example i am saving the output of Get-Process in to variable $process.

$process = Get-Process

and when you see the variable $process and you can see that it contains the Output of Get-Process.

Assign a new value to variable.

use = equal to {equal assignment operator} to assign a new value. When you assigns a new value to variable the new value overwrites the old one.

In below example you can see that the old value of $name was "aman" but when we assigned a new value to $name we can see it overwrites the old one.

Adding Multiple lines in single variable

we can use += {Plus Equal Assignment operator} to add or assign multiple lines to the variables. This is quite handy when you write

$body = "This is a Email "

$body += " Generate from Server"

$body += " Which is located in India"

Remove variables

To remove variable we can use remove-Variable cmdlet and after that provide variable name .

remove-Variable body

let check if the variable exists. Nope nothing there.

Note: The value in variable are static and remain constant until you not changed them. For example you stored the value of Get-Process in a variable and after that you open notepad or any other app, the entry of new launched app wont be in the variable.

Wednesday, April 11, 2012

When new users start working on PowerShell the the main problem which they have faced is basic of PowerShell or we can say basic of any scripting language.

In this "PowerShell basics" tutorial series we are planning to cover as much as basics of PowerShell we can. These basics are not in any particular manner. The things which come in my mind and start covering them.

In the Part-1 we are covering "Single Quotes (') and Double Quotes ("").

In PowerShell we can use Single quotes and double quotes to access the folder and files which have space in there names. Without "single or Double quotes" PowerShell consider blank space as separator.

Have a Look.

Try to Get-Childitem of "Program files" with out space, and you will get an error that it not able to find "C:\Program". As i mentioned before that without quotes PowerShell treat "blank space " as separator.

Now lets wrap the the "Program Files" in Double or Single Quote. And this time it works Perfectly.

we can also use the single quotes to achieve the same. Let see another example.

I have a text file named as " Space test.txt", lets try to open it without quotes first.

OOps !! same error..

Wrap it Single or Double Quotes, this time lets wrap it in a Single quote and see what will happen. Working :)

But..the main difference in Single and Double quotes are described below.

"Double Quotes"

The Main Difference between "single and Double quotes" are that Variable are expands in "Double Quotes" but they don't expand in single quotes.

$name = "Aman"

In variable $name i am assigning a value "Aman" let see if variable expand in $name.

You can see that in Write-Host "My Name is $name" , $name is changed with the value "Aman"

'Single Quotes'

The Single Quotes mainly used to mention the path to files and folders. I already mentioned the main difference above between "single and double" quotes that variables don't get expand in Single Quotes.

Let's look at same above example but with single quotes.

$name = "Aman"

You can see that in Write-Host "My Name is $name" , $name is not changed, that remain constant.

Wednesday, April 4, 2012

This is a very common task which is nearly performed by every End-user and IT Admin and Engineers. Normally we use "IPCONFIG /Release" to release the IP-address and "IPconfig / Renew" to renew or getting a new IP Address.

In some powershell script you may need to do this ip config /release and renew task. We can use native "IPconfig" command to achieve this. But there is an another way to do the same. Using WMI.
Download the script from here :

In variable $ethernet we are querying class Win32_NetworkAdapterConfiguration and using where-Object cmdlet we are choosing those adapter which have IP Enabled and have DHCP Enabled selected in there IP Configuration

than on each adapter we are using wmi method ReleaseDHCPLease() first and then RenewDHCPLease() .