Introduction to Windows PowerShell Scripting



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.


Similar Articles