SIGN UP MEMBER LOGIN:    
ARTICLE

Introduction to Windows PowerShell Scripting

Posted by Sateesh Arveti Articles | Windows PowerShell March 16, 2009
This article explains about scripting in Widnows PowerShell.
Reader Level:


In previous article, we had discussed about PowerShell and its commands. In this article, we will look into scripting capabilities of it. Normally, we use to write a set of commands (cmdlets) to perform a complex task. Sometimes, we might need to do that complex task repeatedly. Instead of typing those commands everytime, we can save them to a file called as script file. We can run the script file, when ever we want to perform that task. The extension for PowerShell script file is ".ps1". We can run a script by using command prompt.

PowerShell scripting language is having rich set of constructs to create simple to complex scripts. It supports looping, branching and variable assignment etc. PowerShell is having its own scripting language, instead of reusing existing languages.

In order to avoid spreading of malicious code in the form PowerShell scripts, it uses execution policy. This execution policy allows us to determine whether scripts can run and they should include digital signature or not etc. In view of security issues, none of the PowerShell scripts run on double-clicking on its script file. We can scripts to automate the tasks that we use to do frequently. Let's look into the syntax and constructs available in this scripting language.

Arithmetic Operators:

+  addition, concatenation
-  subtraction
*  multiplication, string repetition
/  division
%  modulus

Assignment Operators

=  Assigns a value to a variable
+=  Adds a value to a variable
-=  Subtracts a value from a variable
*=  Multiplies a variable by a value
/=  Divides a variable by a value
%=  Performs a modulus on a variable

Functions

function [MyFunction]
{
    write-object $args[0]
 }

Comparison Operators

-eq Equal
-ne  Not equal
-gt -ge Greater than, greater than or equal to
-lt -le Less than, less than or equal to

Logical Operators

! and -not Not a single value
-and And two values
-or Or two values

Comments

# This is a comment.
for
for ($i = 0; $i -lt 5; $i++) {write-object $i}
if/elseif/else
if (condition) {...}
elseif (condition) {...}
else {...}
We can get a complete list of constructs from here.
Let's see how to write a sample script file. This script file will open an IE and logins into a site. Open your notepad and type below code in it:
function main
{
write-host "Opening IE 'n"
$browser = new-object -com "InternetExplorer.Application"
$browser.visible = $true
write-host "Opening c-sharpcorner.com"
$browser.navigate("http://www.c-sharpcorner.com")
[System.Threading.Thread]::sleep(15000)
write-host "Logging"
$document = $browser.document
$uname = $document.getElementByID("UID")
$uname.value = "aaa"
$passwd = $document.getElementByID("PWD")
$passwd.value = "aaa"
$loginButton = $document.getElementByID("btnSignIN")
$loginButton.click()
}
main

When we run this script file in PowerShell console, it will open the site and logs into it. This is an example of Web automation using scripts. In the same way, we can automate many tasks.

If you get an error message shown below, on running any script files. Than, we need to issue this command "Set-ExecutionPolicy Unrestricted" to run scripts.

1.gif 

I am ending up the things here.

In next article, we will look into creation of user defined cmdlets. I hope this article will be helpful for all.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor