I’ve been working on some things for work recently which have me deeply buried in the Windows command line again, both writing applications and scripting/automating actions against various systems on our network.
**Backup your Files to a Remote Computer**
One of the things we all need to do is keep backups of our data.Â It’s easy enough to re-install your OS and all your apps, but if your system dies and you lose your data, the results can be heartbreaking to say the least.Â So be proactive and schedule yourself a backup using xcopy.
**xcopy "c:documents and settings<username>My Documents" "f:<username>" /C /D /E /H /Y**
So lets examine what this does.Â Normally the copy command can only copy single files, and doesn’t handle large file structures very gracefully. This is where xcopy steps up to fill the void.
* /C — Continue to perform the backup even if an error is encountered (like a file that’s currently open/locked by another program)
* /D — Copy only files which are newer than the ones on the destination drive (Don’t copy that 1.2GB movie file that you backed up last week)
* /E — Copy all files and sub-directories under the one you’ve selected including empty ones (To exclude empty directories, use /S instead)
* /H — Copy Hidden and System files (use this to make sure you catch those thumbs.db files that hold thumbnail caches for your pictures)
* /Y — Automatically override destination files without prompting
**Get a list of all your MP3 files (or anything else!)**
Once in a while you may want to generate a list of files from your system.Â This isn’t always an easy thing to do, particularly if those files are buried in a large series of well-organized subfolders.Â But there is a way.
**dir /B /S "c:documents and settings<username>my music*.mp3" > "c:documents and settings<username>my documentsAllMyMusic.txt"**
The dir command is used to get a list of files and typically display it to the screen.Â By changing where the output of the command goes using the greater-than (>) symbol you can print the output of the dir command (or any other command for that matter) to a file.
* /B — Show a bare directory listing, files only no other information or headers
* /S — Recurse through sub-directories to find all instances that match
In this case we set the search up to look through the My Music folder.Â If you had already pointed your command prompt to the location you wanted, you could leave off the folder location, and just use a file string (*.mp3, *.doc, DSCN*.jpg) to search for the files you want.
**Find out who’s logged in**
This tip is aimed squarely at administrators that would like to know who (if anyone) is connected to a given computer.Â If that computer is running Windows XP, Windows Server 2003 or a later OS this can be discovered by way of a couple of Terminal Services commands.
**qwinsta /server:<hostname or IP address>**
This will return a list of user IDs and will also indicate which session they’re connected to.Â The console session (session 0) is the physical UI of the server.Â That means that a person who is using the console session may in fact be in physical contact with the system (they may not too, as this access can be achieved remotely as well).Â Session IDs higher than 0 indicate a connection using a MS Terminal Services client (mstsc).
If a user is connected to a computer, but has simply left the session disconnected (consider this an un-tidied session that’s no longer wanted.
If you find that you want to terminate a user’s session, this can be acheived through another command:
**rwinsta /server:<hostname or IP address> [0|1|2|...|n]**
By entering the name of the server, and the numeric session ID you can forcibly terminate a user’s session.Â This can be much easier than trying to attempt a reboot of the server… particularly if the server is providing other services on your network.
So there you go, a few quick command line tips to help you become more productive with the Windows command line.