Power to Build

Home » Misc » Useful Windows Shell Commands

Useful Windows Shell Commands

Here are some nifty Windows commands you can use on Command line. Some of them are built into the OS Shell (Command,exe), others, separate executable. Some commands are brand new in Windows 7 (or Vista).

CACLS

Similar to CHMOD on *Nix. I haven’t found much use for it yet, though it’s really powerful. My Windows 7 is claiming this to be deprecated (Windows 7) and suggests to use ICacls as of Windows 7.

See here for more info on usage.

CHOICE

Not used this yet, but seems to be powerful. You can actually create your own menu for any batch file.

Choice /C "12345" Results in a option list of 1 - 5:
c:\> [1,2,3,4,5]?
It doesn't get out unless you type one of the options.

C:\>choice /c "ABCD"
[A,B,C,D]?
C:\>choice /c "@#$%"

ERROR: Invalid choice. The valid choice characters are: a-z, A-Z, 0-9 and ASCII values of 128 to 254.

CMDKEY

Lists the stored id and password for all the connections from your PC. For e.g., if you mapped a lot of drives to point to network shares, they will be listed here.

cmdkey /list

FINDSTR

I use this a lot. Both in Scripts and in command line. This is very much like Unix grep, only with quirky syntax. For e.g., to do grep -i, you type findstr /i. 

 

Here is a sample usage:

Netstat /a | findstr 9000

If I want to find out, if anything (typically my EAServer) is running on port 9000.

 

MKLINK

New in Windows 7 (or Vista)

You can use this command to make symbolic links on Windows, both for files and directories. This command is included in command shell.

NETSTAT

I use this a lot these days to troubleshoot connection issues. Particularly, during our recent upgrade/migration of PB/EAS environments, several things went wrong. To be able to find out if the handshakes did happen, I use this a lot – sometimes on both sides, server and client. You can use this for any # of connectivity checks:

Connection to databases (EAServer conn. caches), client to Server connectivity, Web browser to server, Web server to App server. Anywhere there is connectivity issues between 2 software/servers, this command can be helpful.

POWERSHELL

Though it’s been available in XP, in Windows 7, it comes pre-installed. Really powerful scripting environment. Supercedes VBScript. Why they have to change the syntax totally from batch files or VBScript, beats me. But, it’s supposed to be object oriented shell, so you can actually invoke .Net objects as well.

But, I was able to do some simple tasks using this command:

For e.g., When I wanted to display some text in bold yellow in my my PB version switching script, I added the below lines:

:printBright powershell -Command echo Powershell | find /i "Powershell" >nul && ( powershell -Command Write-Host "%*" -foreground "Yellow" ) || ( @echo %* ) goto:eof

Now printbright is sort of a function in Window Batch file. You use it as follows:

 CALL :printBright User Path is adjusted for PB %PBVER%

Now, in the function, I used powershell if found and if it failed, then I simply echo it.

QUERY

New in Windows 7 (or Vista and 2008 servers)

(Though the page says it’s only Windows 8 and Visa, it’s actually there in Windows 7 as well.)

There seems to be variations of this command in different flavors of Windows. In Windows 7, only the below options are available.

query process query session query termserver query user
C:\>query user

USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME

>samV console 1 Active none 08/25/2014 8:37 AM
query process

lists the processes currently running. You may be able to use this command instead of tasklist, to check if a process is running. I have not tried this yet.

ROBOCOPY

This is a powerful copy command for Windows command prompt. This has all the options you always wanted in XCOPY and it didn’t. This is not part of regular Windows install, but you need to download it separately. I think I downloaded the Windows 2003 server resource kit from here. There is even a GUI for Robocopy here. There is also a Sync Toy available in Windows 7 here.

I used this a lot to copy log files from servers to my machine, where I can run scripts to analyze the logs. For e.g., following command copies all the changed files to my local directory. See the link above to get a complete listing of all the options.

robocopy T:\web_log web_log /xo /XX /NJH /NJS /NC /NS /NP /NFL /NDL /s

The parameter list is huge and somewhat cumbersome. For e.g., all those /N’s are basically to turn off logging and/or messages. But it works great!!

SETX

This is a command you always you had in Windows. There is SET, but it only sets within the command shell (or batch file) you are in and loses the values the moment you exit the shell. How do you change it for the machine permanently? That is, how do you change it in the Environment variable? SETX is the answer.

In my PB version switching script, I used it to set the current version selected in an environment variable as below:

 SETX PBVER %PBVER%

You can even use it to update from registry values (HKLM, HKCU), even remotely. Only problem with SETX is that what you set in the environment variable is not set in the current shell. So, you have to issue a SET along with SETX to keep current shell in sync with the new value in environment variable. This is why I do,

SET PBVER=%1 upfront.

SUBST

This is an old command. The reason I included here is to mention about a unique problem you may face in Windows 7, if you are an Administrator. When you go to command prompt, and you do subst to map some drive letters to local directories, you have to be careful about the mode you are in. The mappings you do in normal mode and Administrator mode (when you command prompt as Administrator) are totally separate. They 2 shells almost don’t see each other.

This is very much like sudo in Unix. Only problem is, in Windows 7, when you run any program, it almost always runs in normal mode, even if you are logged in as Administrator. This means all programs including Windows explorer may be running in normal mode, which won’t see any mappings you did with, while “Run as Administrator”!!! This has been a big gotcha for us.

TASKLIST

Lists Processes currently running including services. This is a separate program that comes with Windows. I used this in my post here; In the beginning of the script, I use this command to check if PowerBuilder or EAServer are running before I switch to a different version.

WHERE

Love this command. Like which command on *nix systems. Only, this command searches for any files, not just executables.

For e.g., looking for a specific ini file:

C:\>where its_client.ini
C:\Sybase15\PowerBuilder 15.0\ITS_Client.ini
c:\Sybase\PowerBuilder 12.5\ITS_Client.ini

It only searches the system path specified in PATH environment variable. If you want to search, use dir <filename> /s.

WHOAMI

It’s been available since XP, but I only found use for it in Windows 7. Especially when we were dealing with Active directory and group policy etc.

whoami shows your logon id.
whoami /all shows all the groups and privileges your id has

Other Tools

ELEVATE

If you don’t already have this tool, I suggest you run and get it!! Particularly, if you do a lot of command line activities and constantly have to “Run as Administrator”. This tool comes in handy.

Iin Windows 7 (also Vista), if you are an Administrator, you are running most of the commands in normal user mode. The programs do not automatically assume Administrator rights. To do that, you have to right click on a program and choose “Run As Administrator” to force the admin mode. If only, Windows provided a commandline option for this!!!!

The concept is simple, if you can somehow add “RunAs” while you are running a command, it gets elevated to highest user level available. As long as you are an administrator, you don’t even need to enter ID/Password for administrator. (where as, “Run As Administrator” option in Windows does). But, Windows did not provide this and caused a lot of issues for us.

I wrote scripts to automate software installation on the new Windows 7 machines, and because we, developers, are all automatically Administrators to our PCs, I had to constantly use “Run As Administrator” option to have full rights. I couldn’t be sitting there and doing this for each script, right?

I looked for the right tool and I found Elevate. There are zillion implementations out there (see here), I preferred the one I chose for simplicity in command line options. You can get it from here.

So, whenever I need a command prompt with highest possible user level, I type:

elevate -k

This gives me a brand new shell with Administrator privileges. Type elevate on command line to see other options.

PATHED

I used this to edit PATH environment variable in my scripts. This lets add/remove/chage some entries in the PATH variable. I wish this was actually part of windows.

Simply typing pathed on command line (as long as the command is in the path), gives you a list of paths. No more a messy blob semicolon separate path dump. I wish Windows’s SETX could implemented some of these functionality.

There are several different implementations available out there, incidentally all called, PATHED. I picked the Freeware written by Gerson Kurz (http://p-nand-q.com). It is part of the gtools download.

C:\>pathed

PATHED - Version 3.2

Freeware written by Gerson Kurz (http://p-nand-q.com)
 00 C:\Sybase\Shared\PowerBuilder
01 C:\Prog32\Windows Resource Kits\Tools\
02 C:\Perl64\site\bin
03 C:\Perl64\bin
04 C:\Prog32\Merant\vm\win32\bin
05 C:\Prog32\Merant\vm\common\bin\win32
06 C:\Program Files (x86)\Intel\iCLS Client\
07 C:\Program Files\Intel\iCLS Client\
08 C:\Program Files (x86)\AMD APP\bin\x86_64
09 C:\Windows\system32
10 C:\Windows
11 C:\Windows\System32\Wbem
12 C:\Windows\System32\WindowsPowerShell\v1.0\
,,,,,

Lets you edit path at the machine/user level.

I used this command as below in my PowerBuilder version switching script (essentially to remove previous version of PB from (user level) environment variable and add the new version’s path in.)

@ECHO User Path is adjusted for PB %PBVER% pathed /user /remove "c:\sybase\PowerBuilder 12.5" pathed /user /remove "c:\sybase\PowerBuilder 10.0"
REM pathed /user /slim /remove %OLDPBDIR% /append %NEW_PBDIR% pathed /user /slim /append %NEW_PBDIR%

SHORTCUT

Did you ever want to create shortcuts from command line? I did. When I created install scripts for all the software to install to the Windows 7 machine, I had to /create/replace some shortcuts. For that, I used this tool I found here. It lets you create a windows shortcut for any program and set an icon to it. You can also query an existing shortcut.

For e.g., to see what a shortcut to Opera sitting on a desktop look like, I type:

C:\Users\samv\Desktop>shortcut /f:opera.lnk /a:q

and here is the output:

[opera.lnk]

TargetPath=C:\PROG32\Opera\launcher.exe
TargetPathExpanded=C:\PROG32\Opera\launcher.exe
Arguments=
ArgumentsExpanded=
WorkingDirectory=C:\PROG32\Opera
WorkingDirectoryExpanded=C:\PROG32\Opera
RunStyle=1
IconLocation=,0
IconLocationExpanded=,0
HotKey=0 (None)
Description=

The command completed successfully.

They syntax and options seem non-conventional. For e.g., above command used /A:Q to mention query mode, but it works great!

References

http://serverfault.com/questions/3780/useful-command-line-commands-on-windows

http://pcsupport.about.com/od/commandlinereference/tp/windows-7-commands-p1.09.htm

http://www.windowsnetworking.com/articles-tutorials/windows-7/New-Command-Line-Tools-Windows-Vista-Windows-7.html

http://wintellect.com/blogs/jrobbins/elevate-a-process-at-the-command-line-in-vista

http://code.kliu.org/misc/elevate/

http://www.optimumx.com/downloads.html#Shortcut

http://technet.microsoft.com/en-us/library/ff950685.aspx

http://www.thewindowsclub.com/synctoy-synchronize-files-windows


Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: