3/27/2008

Command @ Windows - Part I

Since it is first introduced to the world, Windows has got the reputation that - "it is NOT CUI friendly". And it has been hated by many talents from *Nix world for decades of years.

As time goes by, things get better. Microsoft finally realized that a good CUI (a Command set and a Shell environment) is very very important for a decent Operating System, especially for an OS that aims to Server market, where automation is one of the biggest concerns. Consequently, Microsoft shipped his latest Shell environment - "Power Shell"(I'd like to call it as POSH) and lots of useful commands. In this series of articles, I will introduce some of them that interest me a lot.

First, you can go HERE, to get a full list of available commands in Windows system. Most likely, it will guide you to a TechNet url. It's full and in detail, but somewhat boring ......

1. shutdown - Shutting down the Machine
  There is a "reboot" in *Nix world, while "shutdown" in Windows. But both of them can do "reboot" AND "shutdown", interesting?
  There are mainly two types of parameters,
  operation type:
    /l - log off
    /s - shutdown
    /r - reboot
    /h - hibernate
  Misc option:
    /t xxx, time to wait before the action happen, xxx is in seconds
    /m machine_name, the machine to shutdown
  One thing need to mention is that if there is no option given, it is the same as "shutdown /?", and also you must give at least one command parameter to make it really shutdown.

  You can type "shutdown /?" for the detailed usage information.

2. findstr - Find string in files using Regular Expression
  It's an amazing tool, something like "grep" in *nix world. The command syntax is: findstr [optional parameters] string_to_found [optional files].
We can category its parameters into 2 types,
  About match pattern:
    /l - use search string literally
    /r - use search string as regular expression
    /i - ignore case when searching
    /b - match at the beginning of a line
    /e - match at the end of a line
    /s - search files in subdirectories
  About display formate:
    /n - display line number of found line
    /o - display offset of found string
    /m - display file name only
  If you don't give the search file list, it will search the content from stdin. This is a good feature to use in combination with command pipeline. For example, we can use:
dir /n /b /s d:\ | findstr /r /i /e ".*\.pdf"
to list all pdf files and its full path in D drive. And we can use:
dir /n /b /s E:\ | findstr /r /i /e ".*wcf[^\\]*\.chm" to find chm ebooks in E drive, whose file name contain "wcf". It's very fast and cool!

Here are the summary on Regular Expression syntax:
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
^ Line position: beginning of line
$ Line position: end of line
[class] Character class: any one character in set
[^class] Inverse class: any one character not in set
[X-y] Range: any characters within the specified range
\X Escape: literal use of metacharacter X
\<xyz Word position: beginning of word
xyz\> Word position: end of word

3. where - find it in PATH
  It's used for search too. But there are two things that make it differ from findstr:
  a. It only searches directories listed in PATH variable
  b. It can't recognize Regular Expression stings, just string with wild char * and ?.
  This is a good tool to find which specific executable is used when you use a command found in PATH. For example, where cmd.exe tells you where the "cmd.exe" is stored.

4. whoami, hostname, winver/ver, systeminfo - all things about yourself
whoami - currently login user
hostname - name of current machine
winver/ver - windows version information
systeminfo - system information

No comments: