Writing Your First PowerShell Command

In this tutorial, you will be writing your first command in Windows PowerShell. At the end of this tutorial you will learn how to fire up PowerShell and get a small introduction to the syntax used in its environment.

Before we begin, let's go ahead and abbreviate PowerShell to PSH for the sake of not repeating it continuously.

Setup

Click on your start button and type in Windows PowerShell. If you are a frequent user, it should most likely show up on the most used programs list. Go ahead and open Windows PowerShell (not the ISE, we will cover the GUI in another tutorial). You should see a screen that appears like so:

Type your Username and Password in the white block. Throughout this tutorial, blocks will be used to maintain privacy of the user.

STEP one.

Analyze the environment. The console printed out the name of the program, the Microsoft copy right from 2009 (only if you are using PowerShell 2) and the first line after that should read the user who is logged in to the account and using the program.

STEP two.

Next to the first line enter:

Example


$str1="Hello"
     

Copy and Try it

Then press enter.

A new line should appear and will allow you to add more onto a new line, enter:

Example


                    $str2="World"

Copy and Try it

Then press enter.
Once again you will notice that a new line appears and that it will allow you to type in more.
Type in the following code:

Example

            
write-output $str1 + $str2
                

Copy and Try it

Then press enter.

You will notice that the console prints out Hello World with a "+" symbol between the two words. Where did everything you typed go, you ask? The system took the string values you gave it and stored them. You now have variable $str1 of type String of course and same for $str2, both containing two different strings. When you wrote the write-output command, you told the program that you want the two strings called and displayed to the console simultaneously.

Your screen should then appear like so:

Now you have successfully written your first PSH script! To clear your screen, on the new line that was brought up after the display of Hello World, go ahead and type either cls or clear. You will notice this cleared your screen. Both will work of course, cls is a little more beneficial since it is shorter.

A Few Final Words

You are probably saying to yourself not so bad right? It truly isn't and as you follow along with our tutorials you will come to find that Powershell is truly remarkable and powerful!