Beginning with PowerShell

Recently I heard about the importance of PowerShell and decided to give it a try. So here goes my first post.

Unlike the command prompt that comes installed with most operating systems, PowerShell is not available by default. However, you can install it from the Microsoft website.

Here is a good news for Windows 7 users. It is available by default.

Where?


Start - All Programs - Administrative Tools - Windows PowerShell Modules. Location might vary, but that's the default on my machine.

Easiest way still? Start - Run - powershell - OK.


If you find any mistakes, please correct me in the comments section of the blog, so we can learn together. Thanks in advance. :)

Anyways, back to PowerShell.

Anything you enter after a # is ignored. This can be used for adding comments to the script for easier understanding.

As usual, here is the first thing everyone learns in programming. "Hello World".


For displaying "Hello World", the "write-host" command is used. So, if you want to output "Hello World", type the below code in notepad :

# Enter comments after this which the compiler will not read.
Write-Host 'Hello World!'


Now, how do we run this?

Save the file anywhere in the computer with the extension ".ps1". Let's say we call it "Hello", then save the file as "Hello.ps1". Eg : C:\Hello.ps1.

Now, open PowerShell from the location :
Start - All Programs - Administrative Tools - Windows PowerShell Modules

The first time you load PowerShell, it'll take a while to load, but the next time onwards, it'll be faster.

This is the first look :

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\WINDOWS\system32>




You can exit out of "system32" by using the usual command "cd\" and go back to the root.

Let's say we save the file in the location :
C:\PS\

Then, we have to run the "hello.ps1" file from this location to get the output.

How?

Firstly, we need to go into the "PS" folder. For this, use the command "set-location" :

PS C:\> set-location ps

And you enter the PS folder:
PS C:\ps>




 The file "hello.ps1" is located in the "PS" folder. To run or execute this file, use ".\<filename>".

In our example the command will be ".\hello.ps1".









So that was our first attempt with PowerShell.


Happy Learning.


No comments:

Post a Comment