Skip to content

Welcome to pshell.info

Hi and welcome to pshell.info. It’s a blog about PowerShell so what better way to start than provide some information about installing PowerShell and some of the basic functionality.

Download PowerShell from microsoft.com. There are versions for Windows XP SP2, Windows Server 2003, Vista and Longhorn. Of course, Windows Server code-name “Longhorn” has not been released yet so there is no finished version for it.

I installed PowerShell 1.0 on Windows Vista. When you start it from the Start menu you will get the PowerShell window with the blue background. Note that when you start PowerShell from the Run dialog (Windows key + R), you will get a regular looking PowerShell console with a black background. This is because the installation program configures the shortcut with other colors and options. If you would like the same look and feel when you launch PowerShell from the Run dialog, just configure the properties of that window with ALT+Space and the Properties… menu item.

Now let’s see if we can find our way around PowerShell. At the prompt, type the “dir” command. In my case, the result is:

 

Nothing spectacular here, it is just the plain old dir command we know from the DOS days. In reality however, there is no dir command in PowerShell. The command “dir” is just an alias for “get-childitem“. There are many aliases in PowerShell and they are provided to you to make the transition to PowerShell easier. Some of the other aliases are: cd, cls, rmdir, copy, move, type, echo, del, …

If you want to see all of the defined aliases, just type “alias” at the prompt. You will get the full list with the actual PowerShell command next to it. If you just want a compact list of all the aliases you can type “alias | fw -column 4“. That has the following result:

When you are typing commands, command completion is available with the <TAB> key. For example, to go to the directory c:\Program Files, just type “cd \prog<TAB>”. It also works with wildcard characters like * and ?. For example: “cd \prog*files<TAB>”. Of course, you can press <TAB> multiple times to cycle through multiple matches.

In addition to * and ?, you can also use [<char><char><char>…] or [<char>-<char>]. For instance [abz] would match a or b or z. [x-z] would match all characters from x to z.

Enough about aliases and typing commands, let’s get down to some more interesting stuff like cmdlets (pronounced command lets). Cmdlets are the main type of commands you use in PowerShell. We have already seen a cmdlet, namely “get-childitem“. A cmdlet starts with a verb, then a dash, then a noun. There are many cmdlets available so you might want an overview. You can get that with the “get-command” cmdlet. If you use “get-command | fw -column 4” you get a more compact overview:

To continue, I will use the “GET-PROCESS” cmdlet. Instead of that cmdlet, you can also use the “GPS” alias. When you type “GET-PROCESS” at the prompt, you get a list of running processes on your computer. The output of the command might be to long to see at once. Just like in the DOS days, you can pipe the output of one cmdlet to another. To view output page per page, use “GET-PROCESS | MORE“. You can then press <SPACE> to see the next page, <ENTER> to only see the next line, and Q to quit.

Of course, the “More” command is not in the verb-noun format so it must be an alias right? Well, not exactly. The “more” command is actually a function. To see the definition of this function, use “type function:/more“. The function uses the “out-host” cmdlet to do the actual paging. If you do “GET-PROCESS | OUT-HOST -paging” you get the same result as “GET-PROCESS | more“.

Now you know about piping output to other commands, what more can you do? First of all, you can sort the output of a command. For example, type “GET-PROCESS | sort -property ws -descending“. The list will now be sorted on the ws property of each process (working set) and in descending order. To page the output, just pipe the output to more again like “GET-PROCESS | sort -property ws -descending | more“. My result:

But how do you know on what to sort? Luckily, PowerShell provides a lot of ways to discover the properties of an object. Just type “get-process | get-member” and you will get a list of properties and methods of the System.Diagnostics.Process type. Let’s forget about the methods and just ask for properties with “get-process | get-member -membertype property“. You now see a list of properties of a process.

Now that we know how to sort and discover what we can sort on, let’s see how to return just a subset of results. That is where the “select-object” cmdlet comes in. Just type the following command: “get-process | sort -property ws -descending | select -First 5” (select is the alias for select-object).

Now let’s finish off by displaying the results in a different way. Suppose I only want to show the processname and the working set. This is done using the formatting cmdlets. There are several formatting cmdlets available:

  • format-table: display results in a table (alias: FT)
  • FORMAT-LIST: display results in a list with all the properties (alias: FL)
  • FORMAT-WIDE: display results in multiple columns (alias: FW)

To display results in a table, just pipe the output of a cmdlet to the format-table cmdlet. With the -property parameter, specify the list of properties you want to see separated by commas. For example: “get-process | sort -property ws -descending | select -First 5 | FT -Property processname,ws“. The output does not look too good. To fix that, use the -autosize parameter: “get-process | sort -property ws -descending | select -First 5 | FT -Property processname,ws -autosize“. The result:

Stay tuned for more PowerShell content soon.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*