Hey, Scripting Guy! How Can I Learn Windows PowerShell?

ScriptingGuy1

Share this post :

Hey, Scripting Guy! Question Hey, Scripting Guy! I recently ran across the Hey, Scripting Guy! Blog and have been fascinated by the different things you can do with scripts. I now think I can use them to make my life easier as a network administrator. So here is my question: How can I learn Windows PowerShell? I know I can keep reading the Hey, Scripting Guy! Blog, and I will learn things but would like to come up to speed more quickly. Do you have any suggestions?

— TW

Hey, Scripting Guy! Answer Hello TW,

Microsoft Scripting Guy Ed Wilson here. Let’s turn the hands of time back a few years, back to a time before I became a Microsoft Scripting Guy, back before I was a Microsoft employee, back before I was a consultant for a Microsoft Solutions Provider, back to a time when I was just starting my career as an IT pro, and I was working on my Microsoft Certified Systems Engineer—for Windows NT 3.51. There was the Internet, but the World Wide Web was still in its infancy. The Internet was mostly populated by things called Archie servers and Gopher servers. One day, I stumbled across a posting for an e-mail list that was powered by a program called LISTSERV that was dedicated to people who were studying for their MCSE. At first I mostly lurked on the list, reading others questions and savoring the responses. After a few weeks, I gathered up the courage to ask my first question. I did not want to appear to be lazy or stupid, so I looked everywhere I could think of to try to find an answer to the question myself, but the solution was not forthcoming. So I wrote the question, looked at it, edited it, and then finally posted it. Anxiously for the remainder of the evening, I went back to my computer, logged on, turned on my modem, loaded the Trumpet winsock application, and listened to the eerie beeping, squeaking, and other oscillating tones that were at last followed by a cheerful beep. I then opened up my e-mail program, and waited for messages to slowly fill my inbox. There it was—one of the last messages was the answer to my question.

The next day when I arrived home from work, I ran to my study, turned on my computer, and once again performed the ritual of the modem dance. This time I was determined to find a question I could answer. I chose one that I thought I knew the answer to, typed up my reply, and posted. When others on the list replied that my answer was correct, I was hooked. After that, there was no stopping me. I made a point of trying to answer one question a night. By the time I took my first MCSE exam—which I passed by the way—I found I had learned more by helping others than I had learned from my own reading and studying.

These days, of course, you do not have to mess with choosing and configuring your own TCP/IP protocol stack, and in most parts of the world (but unfortunately not all parts of the world) modems are relegated to faxing, out-of-band (OOB) management, and certain call-home applications. A few e-mail lists might still be in existence, but the MCSE list that was once the center of my certification world has gone the way of the Gopher server, the Archie server, and other pieces of that ancient era. Only my 9600 bit/s modem remains from those heady days of experimentation and learning.

But, TW, you asked about learning Windows PowerShell scripting and coming up to speed quickly. A great place for you start is with our Learn page. From there you will find links to webcasts, virtual labs, technology pages, and books. A great book to get started learning Windows PowerShell is Microsoft Press Windows PowerShell Step by Step. It has lots of good script examples and step-by-step exercises that are organized in such a way that it will help you to come up to speed quickly.

On the TechNet Script Center, you can also participate on the Official Scripting Guys Forum, which you can get to by clicking the Forums tab on our home page. Sign in with your Windows Live ID and that is all that is needed to post questions and answers. Your Windows Live ID also gives you the ability to upload scripts to the new TechNet Script Center Gallery, which you will most certainly want to do when you have written your first real script!

This week we are looking at questions that have been posted to the Official Scripting Guys Forum. This user forum is a great place to ask questions related to VBScript or Windows PowerShell. It is also an excellent resource to learn scripting by either reading answers to others questions or by becoming involved in discussions through posting answers. In addition to being a learning resource, the forum is also a fun place to interact with people from around the world who have an interest in scripting. The Official Scripting Guys Forum is free. If you wish to post a question or propose an answer to a question, you will need to log in with your Windows Live ID.

Today, I was at the Official Scripting Guys Forum reviewing some of the questions that had been asked, posting answers, and in general just hanging out when I ran across a pretty interesting question: How can I start a new instance of Windows PowerShell from a script and change the window title of the Windows PowerShell console: The thread is seen here:

Image of the forum thread 

What is interesting in this particular thread are some of the ideas that are suggested. The question involves starting a new Windows PowerShell console from within a script, but the person asking the question also wants to pass an argument to the script, and then change the title that is displayed at the top of the Windows PowerShell console. This question and the answer would apply to both Windows PowerShell 2.0 and Windows PowerShell 1.0.

TW, let’s play with the first solution that was offered by one of our MVPs.

Suppose you have a Windows PowerShell script called Title.ps1 that displays a message that says hello with the person’s name that is passed as an argument. This script is seen here:

Title.ps1

"hello $args"

You are inside a Windows PowerShell console and want to start the script in a new Windows PowerShell console, change the console title to be the name of your computer, and pass an argument to the newly started script. The command seen here is one logical command, meaning it can be cut and pasted directly into the Windows PowerShell console as is, or you could type it as one long command and let it wrap. The backtick character (`) is used for line continuation, and you would not need to type it if you were typing the commands in the Windows PowerShell console and letting it wrap. The semicolon is the new command character, and it is used to introduce a new logical command line. You would use it when typing in the Windows PowerShell console. The Windows PowerShell command is seen here:

cmd /c start powershell -NoExit -Command `

{$Host.UI.RawUI.WindowTitle = "$env:computername";

c:\fso\title.ps1 ‘mred’}

The above command starts a new instance of PowerShell.exe, and it uses the –NoExit switch to cause the newly opened Windows PowerShell console to not close. The –Command parameter passes a Windows PowerShell script block that is a regular Windows PowerShell command like you would type directly into a Windows PowerShell console. The $Host.UI.RawUI.WindowTitle exposes the title property of the Windows PowerShell console. The $env:computername retrieves the name of the computer from the environmental drive.

When you cut and paste the command above, you see double arrows at the beginning of each line. This indicates that you have not yet typed a complete command, and Windows PowerShell is allowing you to continue your typing. It is added automatically because of the use of the backtick character at the end of the first line. You must press ENTER again to cause the command to be executed. This is seen here:

Image of incomplete command

When the command runs, you will see another Windows PowerShell console open. It will run the script and display the host name in the title window. This is seen here:

Image of host name in title window

As for copying and pasting, there are two ways to do it. I prefer to paste in the Windows PowerShell console by right-clicking with my mouse after I have pasted something onto the clipboard. Some people like to use the mouse and access the shortcuts menu:

Image of one way to paste in Windows PowerShell console 

You can use this same technique from within the Scheduled Task tool, or from the Start/Run option. You will need to modify the command a little; instead of using the cmd /start option, you substitute the command PowerShell –NoExit. The revised command needs to be a single line, and I use Notepad to keep track of the command and the changes to the command. This is seen here:

PowerShell -NoExit -Command &{$Host.UI.RawUI.WindowTitle = "$env:computername"; c:\fso\title.ps1 ‘mred’}

The above suggestion was in fact my own contribution to the conversation as it was progressing over at the Official Scripting Guys Forum. As I stated earlier, it is fun to become involved in the ongoing threads. It gives you something to look forward to: kind of like the weekend, but more fun.

TW, this should give you something to think about in your quest to learn Windows PowerShell. I highly advise you get involved with the Official Scripting Guys Forum because you really will learn a lot—at least it worked for me. Join us tomorrow as Scripting Guys Forum Week continues.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, shoot us an e-mail at scripter@microsoft.com or post your questions to the Official Scripting Guys Forum. See you tomorrow. Until then, keep on scripting.

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Discussion is closed.

Feedback usabilla icon