Microsoft

Basic Windows PowerShell commands you should already know

PowerShell combines the speed of the command line with the flexibility of a scripting language, making it a valuable Windows administration tool. Here are a few basic commands you'll want to master.

Over the last few years, Microsoft has been trying to make PowerShell the management tool of choice. Almost all the newer Microsoft server products require PowerShell, and there are lots of management tasks that can't be accomplished without delving into the command line. As a Windows administrator, you need to be familiar with the basics of using PowerShell. Here are 10 commands to get you started.

Note: This article is also available as a PDF download. It was originally published in the 10 Things Blog in December 2010.

1: Get-Help

The first PowerShell cmdlet every administrator should learn is Get-Help. You can use this command to get help with any other command. For example, if you want to know how the Get-Process command works, you can type:

Get-Help -Name Get-Process

and Windows will display the full-command syntax.

You can also use Get-Help with individual nouns and verbs. For example, to find out all the commands you can use with the Get verb, type:

Get-Help -Name Get-*

2: Set-ExecutionPolicy

Although you can create and execute PowerShell scripts, Microsoft has disabled scripting by default in an effort to prevent malicious code from executing in a PowerShell environment. You can use the Set-ExecutionPolicy command to control the level of security surrounding PowerShell scripts. Four levels of security are available to you:

  • Restricted -- Restricted is the default execution policy and locks PowerShell down so that commands can be entered only interactively. PowerShell scripts are not allowed to run.
  • All Signed -- If the execution policy is set to All Signed then scripts will be allowed to run, but only if they are signed by a trusted publisher.
  • Remote Signed -- If the execution policy is set to Remote Signed, any PowerShell scripts that have been locally created will be allowed to run. Scripts created remotely are allowed to run only if they are signed by a trusted publisher.
  • Unrestricted -- As the name implies, Unrestricted removes all restrictions from the execution policy.

You can set an execution policy by entering the Set-ExecutionPolicy command followed by the name of the policy. For example, if you wanted to allow scripts to run in an unrestricted manner you could type:

Set-ExecutionPolicy Unrestricted

3: Get-ExecutionPolicy

If you're working on an unfamiliar server, you'll need to know what execution policy is in use before you attempt to run a script. You can find out by using the Get-ExecutionPolicy command.

4: Get-Service

The Get-Service command provides a list of all the services that are installed on the system. If you are interested in a specific service, you can append the -Name switch and the name of the service (wildcards are permitted). When you do, Windows will show you the service's state.

5: ConvertTo-HTML

PowerShell can provide a wealth of information about the system, but sometimes you need to do more than just view the information onscreen. Sometimes, it's helpful to create a report you can send to someone. One way of accomplishing this is by using the ConvertTo-HTML command.

To use this command, simply pipe the output from another command into the ConvertTo-HTML command. You will have to use the -Property switch to control which output properties are included in the HTML file and you will have to provide a filename.

To see how this command might be used, think back to the previous section, where we typed Get-Service to create a list of every service that's installed on the system. Now imagine that you want to create an HTML report that lists the name of each service along with its status (regardless of whether the service is running). To do so, you could use the following command:

Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm

6: Export-CSV

Just as you can create an HTML report based on PowerShell data, you can also export data from PowerShell into a CSV file that you can open using Microsoft Excel. The syntax is similar to that of converting a command's output to HTML. At a minimum, you must provide an output filename. For example, to export the list of system services to a CSV file, you could use the following command:

Get-Service | Export-CSV c:\service.csv

7: Select-Object

If you tried using the command above, you know that there were numerous properties included in the CSV file. It's often helpful to narrow things down by including only the properties you are really interested in. This is where the Select-Object command comes into play. The Select-Object command allows you to specify specific properties for inclusion. For example, to create a CSV file containing the name of each system service and its status, you could use the following command:

Get-Service | Select-Object Name, Status | Export-CSV c:\service.csv

8: Get-EventLog

You can actually use PowerShell to parse your computer's event logs. There are several parameters available, but you can try out the command by simply providing the -Log switch followed by the name of the log file. For example, to see the Application log, you could use the following command:

Get-EventLog -Log "Application"

Of course, you would rarely use this command in the real world. You're more likely to use other commands to filter the output and dump it to a CSV or an HTML file.

9: Get-Process

Just as you can use the Get-Service command to display a list of all the system services, you can use the Get-Process command to display a list of all the processes that are currently running on the system.

10: Stop-Process

Sometimes, a process will freeze up. When this happens, you can use the Get-Process command to get the name or the process ID for the process that has stopped responding. You can then terminate the process by using the Stop-Process command. You can terminate a process based on its name or on its process ID. For example, you could terminate Notepad by using one of the following commands:

Stop-Process -Name notepad
Stop-Process -ID 2668

Keep in mind that the process ID may change from session to session.

Additional PowerShell resources

About

Brien Posey is a seven-time Microsoft MVP. He has written thousands of articles and written or contributed to dozens of books on a variety of IT subjects.

14 comments
vegesm
vegesm

PowerShell is very useful for automatization. And much better than any unix shell due to its object oriented design,

sabza
sabza

I have used it to export and import pst files in exchange server, it just makes life very easy for any administrator.

TuneUp Utilities
TuneUp Utilities

Great post, Brien! Thanks for breaking down PowerShell and some of its most basic and most important commands that users can use. I definitely think PowerShell is a great management tool, do you think it???s absolutely necessary for most PC users though?

online
online

Those who say scripts are no good for management have apparently never explored beyond their own little worlds. Unix administrators were using scripts for system administration before Windows was a gleam in Bill Gates' eye. Almost every administrative task is done more than once... a well-written script can make any task simpler. Plus, with newer server products, I understand some administrative tasks no longer are accessible from the GUI. Powershell is an essential tool for Windows system administration, just as it has been for *nix administration for years.

Craig_B
Craig_B

I highly recommend getting the Quest ADManagement cmds at http://www.quest.com/powershell/activeroles-server.aspx. This adds a lot of easy to use cmdlets. Other basic commands: | pipe, send the object to the next object Example: get-service | select-object name, status out-gridview Sends output to an interactive table in a separate window. Example: get-service out-gridview, then you can search/sort on different properties profile.ps1 Create a profile.ps1 script that runs every time you launch PowerShell. Add Snapins, map drives, etc. You can place it in c:\users\UserName\Documents\WindowsPowerShell folder. format-list * Use this to find out what an object will output to the host. Example: get-service get-service spooler | fl * Now that know something about the object you can then do this: get-service spooler | select-object name, ServicesDependedOn get-member Sends output to an interactive table in a separate window. Example: get-service | get-member The Properties is the information stored about the object. The Method is things you can do with the object. One of the Methods in the example is stop, so now you know the service can be stopped. I find that PowerShell is a very useful tool.

Alexander Freund
Alexander Freund

I don't know any of these commands and I don't know how they should help me with my regular work. It seems that this more academic. I mean I'm using PowerShell to automate some installation but I think we are not so far that PowerShell is like Unix Shell where I log on to the system and get all the information through the shell. For this I stay with the gui.

douglas_n
douglas_n

Hello Mr Kaelin WinXP Home doesn't seem to accept any of these command from the command line. start | command | command line Says GET-HELP is not a recognized as an internal or external command, operable program or batch file. So I must be doing something wrong. I also tried to set-permissions but that command was not recognized either. This stuff is very interesting from a simply curiosity perspective. So, what am I doing wrong...I am not logged in as administrator tho I do have full permissions. Thanks Doug

DNSB
DNSB

For home users, it's not going to be much use however for work, where I used batch files for years to handle repetitive tasks, I'm now using Powershell to do many of the same tasks plus tasks that the standard command line utiltites did not cover. For some tasks, a GUI is fine but when it comes to those repetititve tasks, spending the time to generate a script that the computer will execute without getting bored and making errors is worthwhile. Much the same way, I use the GUI on our Linux servers to do some minor tasks but the repetitive stuff is pretty much all scripted.

Tim Walker
Tim Walker

You might need to install it. Powershell is not the command line. Go to Microsoft.com/powershell

Alexander Freund
Alexander Freund

I'm sorry but what batch files are you using? In a productive environment you can't rely on scripts! We do everything through services like scom and others. You can create bulk users with script or install servers with scripts but you will not run diagnostic scripts.

hondafrank
hondafrank

I'm sorry but what productive environment are you running? Scripting is much more powerful than any GUI, not to mention time saving. Every one of my servers runs some type of script (may not all be PowerShell, but they all use scripts). And yes you will run diagnostic scripts as well. I'd much rather write a script to collect diagnostic information than to sit and babysit a server

Editor's Picks