Windows PowerShell Scripting Course
You are becoming more familiar with Windows PowerShell and recognizing its benefits. This month’s column will be long. This article will give you a quick overview of Windows PowerShell scripting and how to create parameterized scripts. I will be focusing on specific topics over the next few months that build on this foundation.
You might feel that this is too complicated if you are not familiar with running Windows PowerShell commands from the console. However, you can still learn how to do it. You should be familiar with Windows PowerShell security features. You should be familiar with execution policy and the settings you are using. You might not be able to distinguish between “RemoteSigned”, and “AllSigned” and why one may be better than the others.
You must also know how to run scripts in the shell. Also, you should remember that you must always provide a path name and filename to execute a script. You should also be able to distinguish between the Integrated Scripting Environment, (ISE), and the console. Scripts in the ISE run in the global scope. Scripts in the normal shell console have their own scope. I’ll briefly review scope, but you should already know what it is and what it does.
As you read this column, try to follow the examples. Take a look at the examples. You can copy and paste the script examples from the Windows PowerShellISE, starting at line 1. Your line numbers will match the descriptions.
Windows PowerShell Script Files
Table of Contents
A Windows PowerShell script is nothing more than a plain text file with a.PS1 extension. The “1” does not refer to Windows PowerShell’s version, but the version of the language engines. Windows PowerShell versions 1 and 2 use the same language engine version 1. That’s why both versions of the shell are installed into a v1.0 folder under \Windows\System32\WindowsPowerShell.
A Windows PowerShell script doesn’t look exactly like a command line batch file. Running a script is not the same as running the commands yourself in the exact same sequence. To illustrate, open a console window, and type the following.
Now, type the exact same lines into a file or the ISE script editor pane and run the script. You’ll get different-looking results. You start a new pipeline every time you hit Enter in Windows PowerShell. All commands that you have typed are executed in this single pipeline. Windows PowerShell converts the contents of the pipeline into a text display at the end. You have run the two commands in the normal console in two separate pipelines.
Windows PowerShell was capable of creating a unique display for each output set. Both commands could be entered into a script and executed in the same pipeline. Windows PowerShell’s formatting system isn’t powerful enough to create the same output for two different sets. This can be done in the console
These results should look exactly the same as when you ran the script that contained those two commands. Both commands were executed in one pipeline in this case. This is what happened when the script was run.
This means that a script should only produce one type of output. This is largely due to the limitations of the formatting system. There are other things to consider. You don’t want multiple types of scripts dumping into the pipeline at once.
This is the rule that will guide everything else. A script should only produce one type of output. Only one exception is if the script is used as a repository of multiple functions. In this case, each function should only generate one.