Introduction to Windows PowerShell

1 2 3 4 5 6 7 8 9 10 11 12 Page 10
Page 10 of 12


As I mentioned previously in “Command-Line Syntax,” when you type a command, PowerShell looks through list of aliases, functions, cmdlets, and external programs to find the program you want to run.

Because people who do a lot of work at the command prompt are so used to typing commands like cd and dir, PowerShell comes with predefined aliases for many of these familiar commands. Because the names of many of the cmdlets are tediously long, there are predefined aliases for many of the cmdlets that are only two or three characters long. (This should make Unix users happy!) The idea is that you’ll find that there are just a few commands you tend to use over and over. After you know which these are, you can look up their short aliases and use those to save time typing.

One serious limitation of aliases, though, is that they can map only one command name to another. An alias definition can’t include command-line arguments. I would really like to define an alias named h that issued the command cd $home, which changes back to your user profile “home” directory, but this isn’t supported. It’s really unfortunate.

How to Get a Listing of Aliases

You can see a list of all the built-in aliases by typing alias (which, as you’ll see, is an alias itself, for the real command name get-alias). I find it easiest to view this list in a text file by typing two commands:

alias >x <br> notepad x

You’ll notice the alias named %, for ForEach-Object, and the alias named ?, for Where-Object. The latter, used in that “delete old files” command example I gave earlier in the chapter, would look like this:

dir | ? {$_.LastWriteTime -lt (get-date).addmonths(-6)} | remove-item

Personally, I don’t think it this reads well, but if I got used to typing it, it might save some time. It’s up to you whether you want to use shortcuts like this, so nobody’s forcing anything on you. The idea is to use the shortcuts that make sense to you and ignore the others.

How to Define a New Alias

You can define a new alias by typing a command like this:

new-alias -name <em>shortname</em> -value <br><em>realcommandname</em> -description <em>“Brief description”</em>

but substituting the name of the alias you want to create for shortname and the command that the alias should run for realcommandname. If the alias already exists, you have to add -force to the command to replace the old one. I show you how you can shorten this command later in this article.

You should know that alias definitions don’t survive when you close your PowerShell window. The next time you run PowerShell, your custom alias will be gone. If you come up with favorite aliases you want to keep, see the section on Profiles at the end of this article.

Navigating Directories and Other Locations

In the command-line world, you use typed commands to navigate up and down through folders and disk drives. Windows PowerShell uses the same cd command as the Command Prompt to change directories, so familiar commands like

cd \Users <br> cd .. <br> cd subfolder

still work. You can use the Tab key to automatically fill in partially typed file and folder names, just as in the Command Prompt environment.

You can also take advantage of directory names stored in PowerShell variables. Your user profile folder path is stored in variable $home, so you can use the command cd $home to return to your profile directory from any location.

Now, here’s something really interesting: You’re used to using cd to move around the file system. In PowerShell, you can also navigate through the Registry!

Here’s how it works: Just as Windows recognizes different drive letters, like this:

c:\users\bknittel <br> d:\saved_pictures\February2010\Yosemite <br> e:\setup.exe

PowerShell recognizes additional drives, which it calls providers, that treat the Registry and the lists of environment variables, aliases, PowerShell variables, defined functions, digital certificates, and Web Services for Management just like disk drives. You can specify paths to these elements, list them with dir, and in some cases even use cd to navigate through them.

1 2 3 4 5 6 7 8 9 10 11 12 Page 10
Page 10 of 12
7 inconvenient truths about the hybrid work trend
Shop Tech Products at Amazon