WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Top 10 Tips for Using Windows PowerShell

by Jeff Cogswell
11/07/2006

PowerShell is Microsoft's newest replacement for the command line. It's still in beta, and the latest version is available free for download here. For us older folks, we've had to deal with DOS in the past, and then we've held on to the CMD.exe program, which is pretty much the same as a DOS command prompt running under Windows. PowerShell is not an updated version of DOS. Rather, it's a complete command-line system that is far more powerful than any command-line prompt Microsoft has given us before. Starting to learn it, unfortunately, can be a bit overwhelming at first. After you've installed it and explored it a bit, the following tips will help make your life much easier.

1. Remember the consistencies

Remember that commands have a verb-noun format and parameter names start with a -. You can use either / or \ in paths.

Probably the most basic, fundamental rule in using PowerShell is that the commands all have a common format. When you remember the format, you can more easily remember the commands.

The format is verb-noun. For example:

Set-Date
Write-Debug
Get-Item
Get-WmiObject

Each of these is a verb followed by a hyphen followed by a noun. Further, the noun is singular, even if it seems like it should be plural. Thus:

Get-Process

returns all the running processes.

But the consistency goes beyond just the names. Parameters are consistent. In the worlds of DOS and Unix, commands either use - or / to denote a parameter name. In PowerShell, all parameter names start with -.

However, path names, on the other hand, can be separated by either a forward slash or a backward slash. You can use either, whichever you're most comfortable with. If you need to combine paths, however, you can easily use the built-in command join-path like so:

join-path c:\ \temp

which results in c:\temp. (This is handy because you don't have to manually remove the extra slashes that might result in just combining the two strings.)

2. Remember the useful commands

Use Get-Command to get a list of commands and Get-Help to find out help on the commands. You can also pass -? as a parameter to a command to obtain help. Use TabExpansion (and, optionally, find a TabExpansion replacement).

Whenever you're using a new language, you want to know the vocabulary. The second release candidate of PowerShell contains 129 commands. That's a lot to learn and memorize. But you'll want to be at least familiar with what's available, and you'll want to make sure you know some of the more common ones. To see all the commands type

Get-Command

One command is Get-Help. You can use this to find out help on a particular command:

Get-Help Get-Member

will display help on the command Get-Member. Or you can pass -? as a parameter like so:

Get-Member -?

The built-in commands also provide different levels of help. The default help provides basic information. You can get more help providing details on the parameters along with examples by adding -detailed to the Get-Help command:


Get-Help Get-Member -detailed

And you can get even more help by typing -full:

Get-Help Get-Member -full

Since many commands are long and we, as computer people, usually don't want to type huge commands, PowerShell includes an alias feature along with several built-in aliases. You can list all the aliases like so:


PS C:\WINDOWS\system32> Get-Alias

CommandType     Name            Definition
-----------     ----            ----------
Alias           ac              Add-Content
Alias           asnp            Add-PSSnapin
Alias           clc             Clear-Content
Alias           cli             Clear-Item
Alias           clp             Clear-ItemProperty
...

The Tab key has a special usage that helps you find commands. If you remember the verb, you can type the verb then the hyphen, and press Tab repeatedly to cycle through the different possible commands starting with that verb. If you type:

Get-

and then start pressing Tab, you'll see Get-Acl, then Get-Alias, then Get-AuthenticodeSignature, and so on.

However, the built-in tab expansion feature isn't that sophisticated, and Microsoft knows it. That's why it made a function called TabExpansion that can be modified. Search the web for PowerShell TabExpansion (no space in the second word) and see what's out there. People have written TabExpansion replacements and there are some pretty good ones out there. Find one that suits your needs. (Incidentally, you can see the source for the TabExpansion function using Get-Content function:TabExpansion.)

Here's a list of some of the commands I find myself using often. When you use commands, you'll probably want to know any available aliases as well.

Command Aliases

Command Aliases
Copy-Item cpi, cp, copy
ForEach-Object foreach, %
Get-ChildItem dir, ls
Get-Command gcm
Get-Content cat, type
Get-Content gc, type, cat
Get-Help help
Get-Location gl, pwd
Move-Item mi, mv, move
Remove-Item ri, rd, del, rm, rmdir
Rename-Item rni, ren
Set-Location cd, chdir
Where-Object where

Get-Command takes some parameters to help you find the commands you need. For example, if you're looking for commands that have the word Format in them, you can type:

 Get-Command *Format*

However, you might only want the commands that start with the verb Format (such as Format-Table). This will work:

 Get-Command -verb Format

That will return the commands whose verb is exactly Format. If you type

Get-Command -verb Form

you will not get back those with the verb Format unless you use a wildcard pattern:

Get-Command -verb Form*

You can similarly specify the noun:

Get-Command -noun Alias

will list all commands dealing with aliases.

There's also a couple handy single-character aliases. % is an alias for ForEach-Object and ? is an alias for Where-Object.

Pages: 1, 2, 3, 4, 5

Next Pagearrow