When I’m collaborating with other users on an issue, I’m always surprised to see how many people don’t really know some of the basic shortcuts to make working with command prompts easier. I guess I have forgotten that not everyone has been working with command prompts since MS-DOS or csh so they don’t have any exposure to any of the tricks. I once was a noob myself, but now I’m showing my age with this post. :)
Often, I’ll see a user type out the entire path to the 12 (or 14) hive or I’ll see him or her retype the entire command that they just entered before. You don’t have to do that! :) No wonder why some people say they loathe the command prompt. They are doing everything the hard way. Today’s tips apply to both the Windows Command Prompt and PowerShell. If you have been using command prompts for years, this post probably contains nothing new for you. You never know though, you might learn something new or see a feature that you forgot about.
The tab key is your friend. Think of it just like you do in Visual Studio. It’s the IntelliSense of the command prompt world. By pressing it, the command prompt fills in file and folder names for you so that you don’t have to type it all. This is useful for completing long folder names such as Program Files or for executing that new download you got with the really long filename. For example to change to the program files folder, you could type the following.
It also works with the change directory command when you use a slash but be sure and put a space after your CD command otherwise it won’t work.
If you didn’t get the folder or filename you were looking for the first time, just press tab again. If nothing matches, the tab key will do nothing or it might beep at you.
In PowerShell, it provides you the name of cmdlets that match what you typed.
This would allow you to cycle through any cmdlets that match such as Get-SPWeb, Get-SPWebApplication, Get-SPWebPartPack. It would be easier to demonstrate this ability with a video, but hopefully you get the idea.
Don’t type that last command you entered just to correct a syntax error! There are a number of ways to execute previous commands. Start with the Up and Down arrow keys. This lets you go see and execute any of the previous commands you issued. The F5 and F8 keys also have the same effect. F5 shows you the oldest command you issued and F8 shows you the last command you entered. Pressing F7 is really slick and shows you a list of all the commands you entered.
F9 allow you to pick a command from the above list by number. I’m not really sure when that would be useful, but its there if you need it.
Copy and Paste
You can copy and paste to and from a command prompt or PowerShell window, but you have to use a mouse to do it! Ack! At least, I’ve never found a way to do it with a keyboard. Unfortunately, Ctrl+C, Ctrl+V and things like that simply don’t work. To paste something in to a command prompt or PowerShell, you have to use the edit menu, which you can find by clicking on the icon in the top left corner of the window. For example, if I wanted to paste a path for use with a change directory command, I could use it as you see below.
There is actually a better way to work with paths though that you will see in a bit. If you want to copy something from a command prompt PowerShell, you can do that too. Go to the edit menu again and choose mark.
Then you just use your mouse to mark the text and go back to the edit menu and choose Copy.
Drag and Drop
I mentioned earlier that there was a better way to get paths into PowerShell. If you have a folder open in Windows Explorer, you can actually drag files and folders directly into a command prompt or PowerShell and it will put the path there. This is great for executing files with long names or for switching quickly to folders with long paths such as the 14 hive.
The Path variable has been around for ages and can still prove to be quite handy. Probably the place I use it the most is to allow me to execute stsadm from anywhere. This was more useful with SharePoint 2007 and is not as relevant any more, but it’s still worth mentioning. What you want to do is add common paths of folders that contain executables that you run often. You can set the path directly from the command prompt, but it you want to make it permanent, the easiest way is to go to your Computer Properties –> Advanced –> Environment Variables. Scroll through the list on the bottom, click on Path and then Edit. In the Variable Value textbox, go to the end add a semicolon (;) delimiter and then add your path such as c:\program files\common files\microsoft shared\web server extensions\12\bin. Here is what it looks like.
When you save the changes, open a new command prompt, and you will be able to execute files from that new path from anywhere. For example, I can run stsadm from anywhere now as shown in the screenshot below.
Sometime, you execute a command and get a lot of results back. Whether it’s a directory listing or a list of processes, you might want a way to make the results more readable. The quickest way to get yourself some breathing room is by hitting the Pause key. You have to be fast though. :) Another way is to use a pipe (|) with the more command. This has been around a while and works with commands prompts or PowerShell. Here is an example.
Get-Process | more
The results look like the screenshot below.
This gives you the results conveniently one page at a time and you can press the Spacebar to view the next page or the return key to view one additional line at a time. If you decide you don’t need to page through the rest of the results just press Ctrl+C. You can use this keystroke to break you out of most commands.
The last thing I will cover is output direction. This has been around at least since the UNIX days and we will see it in modern command prompts. It gives you the ability to redirect all output to a file using the greater than (>) symbol. To redirect the results of the above command into a file we would issue the following.
Get-Process > processes.txt
When you execute it, you will not see any results on the screen.
Opening the file we see the results from the command. It’s a useful technique when you are dealing with lots of results from a command prompt or PowerShell.
If you’re new to command prompts, I hope this post proves to be useful for you. I hope to save people some time and frustration the next time they have to issue some commands. Have some other useful tips to add? Leave a comment!
Follow me on twitter.