The Basics of Creating and Running Windows PowerShell 1.0 Scripts

From Techotopia
Revision as of 20:10, 3 December 2008 by Neil (Talk | contribs) (= Handling Script Arguments)

Jump to: navigation, search

As mentioned in previous chapters, Windows PowerShell is both a shell environment and a scripting language. In the previous chapter we looked at using the interactive shell environment to enter and execute commands. In this chapter we will look at created and executing Windows PowerShell script files.


Contents


What is a PowerShell Script?

A Windows PowerShell script is essentially a file containing a sequence of Windows PowerShell commands, statements and expressions which are to be executed in seuqence to perform a particular task to set of tasks. This is really no different from entering commands one by one at the interactive command prompt, expect that the commands are instead entered into a file which can be repeatedly executed when needed.

An Example Windows PowerShell Script

For the purposes of this example, we will create a variation on the venerable "Hello World" example that has been used to begin teaching the basics of just about every programming languages for the last 30 years or so. To begin, open an editor suitable for writing scripts. This can either be a programming editor, or something as simple as the Windows Notepad application. The purpose of the script is to ask the user for their name and then say hello to them. With this in mind, enter the following statements into the editor:

Write-host "Please enter your name:"

$userName = read-host

write-host "Hello $userName!"

PowerShell 1.0 Script Naming Convention

In order for Windows to recognize Windows PowerShell 1.0 scripts they must have a .ps1 file name extension. Once the script has been written, therefore, save it to your home folder as hello.ps1

Executing PowerShell Scripts

PowerShell scripts may be launched from within the PowerShell interactive environment or from the command prompt. To launch the script at the PowerShell interactive prompt, launch Windows PowerShell (Start -> All Programs -> Windows PowerShell 1.0 -> Windows PowerShell or enter powershell at a command prompt). At the PowerShell interactive prompt enter the following command:

./hello.ps1

The reason for the ./ prefix is to tell Windows PowerShell that the script to be executed is in the current directory. By default, Windows PowerShell will not execute a script in the current directory unless it is prefixed with ./. This is for security reasons and is intended to ensure that the user really wants to run the command from the current directory, as opposed another command with the same name located elsewhere (or built in to PowerShell).

By default, it is likely that an error will be displayed similar to the following:

PS C:\Users\Administrator> ./hello.ps1
File C:\Users\Administrator\t.ps1 cannot be loaded because the execution of scripts is disabled on
this system. Please see "get-help about_signing" for more details.
At line:1 char:7
+ ./t.ps1 <<<<

This is another security measure which prevents the execution of PowerShell scripts by default, primarily to stop malicious scripts of being executed without the user's knowledge. To enable script execution, the following command must first be issued:

PS C:\Users\Administrator> set-executionpolicy remotesigned

Having configured with execution policy, the script may now be executed:

PS C:\Users\Administrator> ./hello.ps1
Please enter your name:
Neil
Hello Neil!

To launch the script from the command prompt without entering PowerShell interactive mode, simply pass the script to the powershell command at the command prompt using the -command option:

C:\Users\Administrator>powershell -command ./hello.ps1
Please enter your name:
Neil
Hello Neil!

Handling Script Arguments

In the above example the user was prompted for their name. Often it necessary to pass one or more arguments to a script at the point that it is executed. This is handled via a special variable called $args which is populated with any arguments which were passed through to the script when it was invoked. Taking our previous example, we can simply reference the $args variable in our script:

write-host "Hello $args!"

When the script is executed we simply pass our anme through as parameter:

PS C:\Users\Administrator> ./hello.ps1 Neil
Hello Neil!

The $args variable is actually an array object which contains an element for each argument passed through. For details on working with arrays refer to the chapter entitled Working with Arrays in Windows PowerShell 1.0.

Default arguments may be specified to handle cases where an argument is missing from the command line using the param keyword. In the following example, we specify that the first argument passed through to the script is to be assigned to a variable named $userName, and that in the absence of that argument, the value of "Stranger" is to be used:

param($userName="Stranger")
write-host "Hello $userName!"

When executed with no arguments, the script uses the default parameter value:

PS C:\Users\Administrator> ./hello.ps1
Hello Stranger!

When an argument is provided, however, this is used in place of default parameter:

PS C:\Users\Administrator> ./hello.ps1 Fred
Hello Fred!

The PowerShell exit Keyword

A Windows PowerShell script either be allowed to run through to completion, at which point it will exit automatically, or an exit may be forced at any point in the script through the use of the exit keyword. For example, the following script exits if the correct user name is not passed through as an argument:

if ($args -ne "Fred")
{
	"I don't know you. Goodbye!"
	exit
}

"Hello $args!"

The following output shows the result of running the script with both the correct and incorrect user names. As illustrated, if the correct name is not entered the script exits before displaying the Hello message:

PS C:\Users\Administrator> ./hello.ps1 Fred
Hello Fred!
PS C:\Users\Administrator> ./hello.ps1 John
I don't know you. Goodbye!