Hey, Scripting Guy! Windows Powershell: An Introduction

Hey, Scripting Guy! Windows Powershell: An Introduction

  • Comments 8
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! Your Windows PowerShell articles have been real interesting, and I have been fascinated by the things you can do. But I need you to start at the beginning. It all looks real confusing to me. How about it?

- TD

SpacerHey, Scripting Guy! Answer

Hi TD,

You know things are only confusing when you do not understand them. We remember very well the first time we downloaded and installed Windows PowerShell. We also remember a painful couple of weeks spent with our heads down over the computer console trying to figure things out back when there were no books written on the subject and the product was still in beta, and even the Help files were incomplete. Ed remembers very well his first thought after installing Windows PowerShell: "Dude, this is different."

This week we will be looking at the basics of Windows PowerShell. Windows PowerShell is installed by default on Windows 7 and Windows Server 2008 R2. It is an optional installation on Windows Server 2008 and a download for Windows Vista, Windows XP, and Windows Server 2003. The Windows PowerShell Scripting Hub is a good place to get started with Windows PowerShell.

So what is Windows PowerShell? For people who come from a Windows background, Windows PowerShell will take some getting used to because it provides a new kind of capability to the environment. On the one hand, there is the Windows PowerShell console that enables you to type commands in an interactive manner. On the other hand, it is a scripting language that lets you write scripts. It is similar to taking the command prompt and putting VBScript inside it. You get two tools in one. It is like a chocolate bar with almonds in it: You get both chocolate and nuts.

The heart of Windows PowerShell is the cmdlets that are included with the product. These cmdlets are built into Windows PowerShell, but can also be written by other people to provide additional capability. Let's open Windows PowerShell, and get information about all the processes running on our computer. The Get-Process cmdlet returns information about the process ID, how much CPU time is being consumed, and memory consumption. The cmdlet is Get-Process, and the results are seen here:

Image of detailed process information provided by the Get-Process cmdlet

 

If we are interested in services, the Windows PowerShell cmdlet is Get-Service. When we run this cmdlet, we get information about the service name, status, and display name. The results are shown here:

Image of the information provided by the Get-Service cmdlet

 

If we want to get the date, the cmdlet is Get-Date, and if we want the culture settings on the computer, we use Get-Culture. But we are not limited to just using Windows PowerShell cmdlets within the Windows PowerShell console. We can also use external commands such as getmac.exe. This is shown here:

Image of both Windows PowerShell cmdlets and external commands being combined in the same Windows PowerShell session

 

To this point, all of our cmdlets have begun with the word Get and have been followed by another word. All Windows PowerShell cmdlet names are made up of two parts. The first part of the cmdlet name is called a verb, and the second part of the cmdlet name is called a noun. These are not strictly grammatical in definition. But the idea can be summarized as follows: "What do you want to do, and what do you want to do it to?" A standard set of verbs makes it easy to remember the cmdlet name. As an example, a common task by network administrators is to stop a process. Many different utilities require us to remember to stop, unload, delete, kill, terminate, or some other permutation of ending a process. In Windows PowerShell it is easy—the verb is always Stop. If you want to start something; you use the Start verb. A listing of the verbs from Windows PowerShell 1.0 is seen in Table 1.

Table 1 Windows PowerShell 1.0 verbs
NameCount

Add

4

Clear

4

Compare

1

ConvertFrom

1

Convert

1

ConvertTo

2

Copy

2

Export

4

ForEach

1

Format

4

Get

29

Group

1

Import

3

Invoke

3

Join

1

Measure

2

Move

2

New

8

Out

6

Pop

1

Push

1

Read

1

Remove

5

Rename

2

Resolve

1

Restart

1

Resume

1

Select

2

Set

13

Sort

1

Split

1

Start

3

Stop

3

Suspend

1

Tee

1

Test

1

Trace

1

Update

2

Where

1

Write

7

You can also use Windows PowerShell in ways that are already familiar to you. For example, if you want to get a directory listing, you can type dir as shown here:

Image of using dir to get a directory listing

 

To create a directory, you can use the md command and tell it the name of the directory you want to create. As soon as a directory is created, you can create a text file by using the redirection arrows to capture the results of a command such as the dir command that was used earlier. This is shown here:

Image of the command to create a text file

 

The text file that was created in the previous command is shown here:

Image of the text file created by the previous command

 

The last thing you might want to do is delete a text file and a folder. To do this, you use the del command to delete a file. The first thing that you might want to do is to change your working directory to the c:\hsgTest folder that was created earlier. To do this, you use a command that is familiar to you: the cd command. As soon as you are in the directory, you may want to get another directory listing by using the dir command. Next, you use the del command to delete the directory.txt file. As shown in the following image, the file name is preceded by a ".\" character. This means that you are interested in the file in the current directory. When you type the first couple of letters of the file name and press the tab key, the ".\" is automatically added to the file name in addition to expanding to the complete file name. This enables you to avoid typing the complete file name. The feature is known as tab expansion.

Image of deleting a file

 

Well, TD, as you can see, working with Windows PowerShell can be as easy as working at the command prompt. We hope that you have enjoyed this introduction. Join us tomorrow as Windows PowerShell Basics Week continues. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Why there no Pictures between the Text????

  • my first course here and I like it :)

    Nicely done.

  • Nice!

  • Nice!

  • Thumbs Up, Well Done

  • i like it. it is writen here very simple so that it´s good to understand. thx.

  • i like it. it is writen here very simple so that it´s good to understand. thx.

  • Very good, it reminds me on the Commandset of dos x.xx