Help! It Says PowerShell…Now What?

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about how to run a Windows PowerShell command.

Hey, Scripting Guy! Question Hey, Scripting Guy! Help! I am reading through TechNet, and I found this command that seems like it will solve my problem, but I do not know how to do it. It says something about Windows PowerShell, but I do not know what that is, or how to run the command. I opened the command prompt, but all it did was give me errors. So where do I type the command?

—WB

Hey, Scripting Guy! Answer Hello WB,

Microsoft Scripting Guy, Ed Wilson, is here. One of the bad things about working at Microsoft, is that time sort of becomes skewed. By this, I mean that it is hard to remain in sync with the rest of the world. What am I talking about?

Well, I have been using Windows PowerShell 5.0 for a long time, and yet, Windows PowerShell 5.0 has not even released. I began using Windows PowerShell before it was even called Windows PowerShell, so I tend to think that everyone is using Windows PowerShell. In fact, the other day, I was speaking at a Windows PowerShell user group, and I pretty much assumed that everyone knew what Windows PowerShell was, and that they were using it. As I found out after the meeting, this is not the case.

In fact, when I was at Ignite in Chicago, I talked to several people who had not ever used Windows PowerShell—and it seemed that they had not even heard of Windows PowerShell. "Strange," I thought.

I pretty much assumed that everyone knows about Windows PowerShell, and has been using it for a long time—at least, that is my world. But Microsoft is not the rest of the world. I even talked to a person at Ignite, who said they were still running Windows 2000 on some of their servers.

Basic Windows PowerShell

Where do I get Windows PowerShell? If you have Windows 7, you have Windows PowerShell 2.0 already installed. If you have Windows 8, you have Windows PowerShell 3.0, and if you have Windows 8.1, you have Windows PowerShell 4.0. If you want to install Windows PowerShell 4.0 on Windows 7, you can refer to this blog post by Windows PowerShell MVP, Teresa Wilson, where she take you through the installation process step-by-step: Install PowerShell 4.0 in Windows 7.

When you have Windows PowerShell installed, you need to launch it. You can do this by simply typing PowerShell on the Windows 8.1 or Windows 8 Start screen. When you start to type, the Search dialog box appears automatically. In Windows 7, use the Start/Search command and type PowerShell.

There are actually two (maybe 4) versions of Windows PowerShell: the Windows PowerShell x86 version and x64 version. Plus there are the same potential flavors of the Windows PowerShell ISE. The Windows PowerShell ISE is the script editor, and for this exercise you do not need to start the ISE.

I like to create a shortcut to Windows PowerShell and place it on my desktop. To do this, I right-click the desktop and choose New/Shortcut from the action menu, type PowerShell in the dialog box, and click next.

When Windows PowerShell first launches, support for scripts is disabled. This is fine because I do not need to run a script right now. All I want to do is to run a Windows PowerShell command.

Running a Windows PowerShell command

As an example, I can obtain a list of all the processes on my system. To do this, I use the Get-Process cmdlet. Cmdlets are Windows PowerShell commands that return information. I can simply type Get-Process (that is a hyphen in between the words Get and Process). By the way, I do not have to capitalize Get and Process, I can type get-process and the command works fine. Here is the command and the output:

Image of command output

Using Tab expansion

I also do not have to type the entire words Get and Process if I do not want to do so. I can simply type Get-Pro and then press the Tab key. When I do this, Windows PowerShell uses what is called Tab expansion, and it will complete the command for me. If I type Get-P and then press the Tab key, I will have to press the Tab key 20 or so times until I finally get to Get-Process. If I type Get-Pr and then press Tab key, I will need to press the Tab key a dozen or so times.

So I have a choice of typing more of the command name, and hitting the Tab key less, or typing less of the command name, and hitting the Tab key more. If I start hitting the Tab key a bunch of times really quickly, and I go too far (meaning I see the command flash on the screen, and then I go to the next one accidently), I have two choices:

  • I can continue pressing the Tab key and cycle through all the options again.
  • I can hold down the Shift key while I hit the Tab key, and it will cycle through the options in reverse.

So if I went two command names too far, I would hold down the Shift key and hit the Tab key only twice.

 Using WhatIf

If I am going to use a command and I do not know what it will do (such as Stop-Process), I can use the –WhatIf parameter. This will permit to see what would happen if I were to run the command for real. Or to think of it another way, “What would happen if I run this command?”

I can use the –WhatIf parameter for any Windows PowerShell cmdlet that will make a change to my system. This means the cmdlet will create something (New), change something (Set), delete something (Remove), or stop something (Stop).

Each Windows PowerShell cmdlet has a two-part name. The first part of the name is a verb, such as Get, Set, Stop, or Remove. The second part of the name is a noun, such as Process or Service. So the two part names help me know what a cmdlet will do and remember the command name.

For example, if I want to start a process, I would use the Start verb and the Process noun: Start-Process. Another good thing is that the cmdlets are (mostly) singular. So it is Process, not Processes, and it is Service, not Services.

Because starting a process will change the state of my computer (by starting a service), it means that I can use the –WhatIf cmdlet. Here is an example:

start-service bits –WhatIf

Here is the command and the output from the command:

Image of command output

If I see a Windows PowerShell command in a TechNet article, and I am not sure what it will do, I take the following steps:

  1. Start Windows PowerShell.
  2. Copy the Windows PowerShell code from the article.
  3. Paste the Windows PowerShell code from the article into the Windows PowerShell console and make sure that I add the –WhatIf parameter to the command.
  4. Press ENTER to run the command. I should see an output similar to that shown in the previous image, which tells me what the command will do.
    • If I like what it will do, then I press the Up arrow to retrieve the command that I just pasted, and I delete the
      –WhatIf parameter and press ENTER to run the command for real.
    • If I do not like what the command will do, I close Windows PowerShell by clicking the X in the upper-right corner of the Windows PowerShell console window. This will keep me from accidently running the command.

For example, if I copy a command into my Windows PowerShell console, I add the –WhatIf parameter, and it returns an output such as I see in the following image, I know that I do not want to run that command. So I close the Windows PowerShell console to avoid making a mistake that will crash my system.

Image of command output

WB, that is all there is to using a Windows PowerShell command. Windows PowerShell Fundamentals Week will continue tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon