Writing Your First PowerShell Script


In today’s article Michael Sammels is going to show you how to write your first PowerShell script. This is just an example script, and doesn’t even begin to tip the iceberg – but we will get to the more advanced stuff later on. For now, we will start with the basic items, and work our way up from there.

This article was created using Windows 10, but if you have Windows 8.1, you should still have the Windows PowerShell ISE (Integrated Scripting Environment) installed. You will need to go ahead and open this. You should be faced with a window that initially looks like so:

PowerShell intro

If you only see the blue section, you will need to go File -> New to open a new document and display the white portion at the top. A basic run through:

The white portion on the top is where you will code your actual scripts. The blue section on the bottom is the PowerShell window, where you can perform you commands and observe the output of your script, and the section docked to the right hand side is a list of the functions PowerShell can do, if you ever need a cheat sheet.

So with that said, let’s dive straight into the script. The first thing I would recommend doing, is adding a comment to your script. I won’t discuss the standards of comments in this article, but what I will say is that in PowerShell, the comments are marked by the hash (#) symbol, and appear as green.

# My First PowerShell Script
# Michael Sammels

This allows you to easily keep track of your scripts. You will want to replace my name with your own, of course, and perhaps add the date your created the script in another comment underneath. While comments will increase the number of lines in your code, and obviously add to the overall file size, the compiler will skip past these when compiling, so the overheads should be low.

The function which we are going to script will allow you to make the computer speak by entering the following command: Capture2

You should note that this will only work in Windows PowerShell and when the script has been loaded in. In future articles we will look at how to compile this into a .exe file so you can run it from command prompt, and in the future we may even wrap it into a Visual Studio project so you can run it from anywhere. Of course, if you wanted to be really fancy you could wrap it into a UWP (Universal Windows Platform) app, so it will work on any Microsoft-based device.

I will code the function here, then we will dissect it line by line. The entire function is as follows

Capture1

As you can see, the function is not entirely that big. Let’s dissect this via line numbers

  1. This line defines we are creating a function, and then goes on to name the function, opening the brackets to allow code to enter
  2. This line creates a parameter, which must take place, and takes a value from the user input. It puts this value into a variable named “Text”
  3. Here we are using the Reflection Assembly and loading the speech assembly (you notice we are not typing out the entire name as this would take too long), and sends the output to NULL (effectively deleting it)
  4. This line creates a new variable named “object”, which loads the Speech Synthesizer from the previously loaded assembly
  5. This line takes the text from the aforementioned variable on line two, and sends it to the synthesizer so you can hear it
  6. This line simply closes the function

And that’s it! Congratulations, you have created your first PowerShell script. Let’s try it out! You may want to save at this point (although it is not required to compile and run). Your window should now look like so:

PowerShell Intro

If you notice the red text in my PowerShell Output (blue section), this is because I tried to run “Say-Text” before compiling, and was hit with the error that the cmdlet (or function) does not exist. Press the green play button, and you should see an output of your script, with (hopefully) no errors, like so:

PowerShell Intro

If this is the case type this into the blue section: Say-Text “Hello from Windows PowerShell” and with any luck, your computer will speak the words you typed!


Comments (3)

  1. Adrian Byles says:

    Valuefrompipline in first list should be Valuefrompipeline, as in the following screendumps

  2. Oscar Perez says:

    Great function, but not for beginners. Waiting for next

Skip to main content