Learning PowerShell – Lesson Five

Share:
PowerShell

PowerShell Lesson Five

By: Steven Aiello

Teach Your Self PowerShell – Introduction to Variables and the PowerShell ISE

PowerShell ISE

In order to go much further with your PowerShell scripting you’re going to need an environment where you can write, test, and save your scripts. For that we will use the free editor that Microsoft provides for PowerShell the PowerShell ISE.

What is PowerShell ISE? According to Microsoft it is:

The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console.

http://technet.microsoft.com/en-us/library/dd315244.aspx

To access PowerShell ISE simple press your windows key and in your “search programs and files” box type in “ISE” and select “Windows PowerShell ISE” from the menu.

Windows PowerShell ISE by using the search menu

Once the application is launched you will see a Windows that looks something like this:

Your screen may look slightly different but the main portions you are concerned with are the editor section which in this screen shot is on top and can be identified by the line number on the left side of the screen and then the light blue section on the bottom where you will see the results of your code that was executed.

As you can see now when we execute the command Get-Process the results are displayed in the bottom pane.

At this point try to locate and execute some commands you have learned in PowerShell ISE on your system.

Script Variables and the Write-Host command

The simplest way to explain a variable is a symbolic name for a piece of information. If you could think about in very simple terms your name as a person could be a variable for your physical person. My name is Steve however I am not a name the name is a symbol for me as a complete person.

To make this concept even easier think about the term your nation’s capital; what word comes into your mind?

For the US the “capital” would be Washington DC

For India the “capital” would be New Delhi

For China the “capital” would be Bejing

This same term capital can refer to many different things. The term capital in this send would be a variable.

Variable Types

Here is a listing of all the variable types in PowerShell:

[int]32-bit signed integer

[long]64-bit signed integer

[string]Fixed-length string of Unicode characters

[char]A Unicode 16-bit character

[byte]An 8-bit unsigned character

[bool]Boolean True/False value

[decimal]An 128-bit decimal value

[single]Single-precision 32-bit floating point number

[double]Double-precision 64-bit floating point number

[xml]Xml object

[array]An array of values

[hashtable]Hash table object

This may seem to be a large and daunting list, however don’t worry we can do a lot with just a few of these variable types. The variables we will start working with will be:

[string]Fixed-length string of Unicode characters

[char]A Unicode 16-bit character

[bool]Boolean True/False value

Write-Host

To see the power of variables we’re going to introduce a new command what will allow us to print data to the screen. This command is:

Write-Host “Steve’s name is Steve”

This command simply writes data to the screen. Let’s look at an example of how this works.

As you can see I’ve used the Write-Host command and put quotes around the text that I wanted to display on the screen. Notice that I’m using double quotes on the outside of the string and I have a single quote on the inside of the string. This is what would have happened if I used single quotes on the outside and a single quote on the inside.

As you can see we get an error. This doesn’t mean we can’t use single quotes on the outside of the string it just means we have to make sure out our quotes don’t conflict with our inner quotes. Here’s an example where I don’t use single quotes on the inside of the string but use single quotes on the out side of the string.

Notice no errors are returned and the command executes successfully.

So now that we know how to write data to the screen how are we going to put a piece of information in a variable and display it back? Just like this.

$VARIABLE = “Something”

Let’s look at a continuation of our previous example:

As you can see we assigned the variable using the syntax:

$NAME = “Steve”

Notice the “$” which indicates that the next word is a variable, then we use the equals sign to assign a string to that variable name. As a note of caution you need to pay close attention to how you use your commas.

If you notice in above screen shot we use double quotes around our variable, but single quotes around the string following the Write-Host command. As you can see these results don’t produce the results you would want. The variable isn’t being properly formatted to print the value of the variable. Again if you’re experiencing an issue with displaying a result of variables remember to check your quotes!

 

Share:

Leave a reply

fifty four − = forty six

This site uses Akismet to reduce spam. Learn how your comment data is processed.