cover

Table of Contents

Cover

Copyright

Publisher's Note

Dedication

Acknowledgments

About the Author

Introduction

Part I: Command Line Basics

Chapter 1: Configuring the Local Machine

Configure the Command Window

Set the Environment

Perform Common Tasks

Obtain Command Line Help

Chapter 2: Making Remote Connections

Configure the Remote System

Use the Remote Desktop Connection Application

Terminate a Session

Chapter 3: Automating Tasks

View and Manage Tasks Using Scheduled Tasks

Manage Tasks Using the SchTasks Command

Part II: Managing Data

Chapter 4: Working with File and Directory Objects

Manage Directory Objects

Manage File Objects

Work with File Associations and Types

Make Data Links

Chapter 5: Administering File and Directory Content

Execute Applications Anywhere

Locate Information in Files

Monitor the File System with the FSUtil Command

Display Data Files

Chapter 6: Managing the Hard Drive

Save Hard Drive Space

Manage the Volume

Maintain the Volume

Manage Partitions

Chapter 7: Securing the Data

Protect Data

Change File and Directory Access

Detect Shared Open Files

Take Ownership of Files

Part III: Managing the Network

Chapter 8: Managing the Network

Get the Media Access Control Information

Interact with the Network Using the Net Utility

Chapter 9: Working with TCP/IP

Manage the Internet Protocol

Use Basic Diagnostics

Perform Detailed Network Diagnostics

Get Network Statistics

Manipulate the Network Routing Tables

Chapter 10: Creating System Connections

Perform Remote System Management

Work with Terminal Server

Part IV: Interacting with Active Directory

Chapter 11: Configuring Directory Services

Manage Directory Services Using the WMIC NTDomain Alias

Manage Active Directory with the DSQuery Utility

Manage the Active Directory Database

Chapter 12: Working with Directory Objects

Create New Objects

Get Objects

Edit Existing Objects

Move Existing Objects

Delete Existing Objects

Part V: Performing Diagnostics

Chapter 13: Monitoring System Events

Create Simple System Events

Trigger System Events

Manage Event Information

Chapter 14: Monitoring System Performance

Add Performance Counters

Manage Performance Logs and Alerts

Create New Performance Logs from Existing Logs

Remove Performance Counters

Convert Event Trace Logs

Part VI: Performing Maintenance

Chapter 15: Performing Basic Maintenance

Configure the Server

Activate Windows

Manage the System Time

Manage the Boot Configuration

Chapter 16: Managing System Users

Audit User Access

Work with Group Policies

Obtain Session Status Information

Get the User’s Identity

Chapter 17: Securing the System

Add Virus and External Intrusion Protection

Change the Verifier Settings

Configure Local Security Policies

Work with General Applications

Chapter 18: Interacting with the Registry

Perform Basic Registry Tasks

Use the SCRegEdit Script

Manage the Registry

Part VII: Creating Batch Files

Chapter 19: Changing the Batch File Environment

Use the CMD Switches

Modify Config.NT

Modify AutoExec.NT

Chapter 20: Working at the Command Prompt

Redirect Command Line Output to the Clipboard

Manage Usernames and Passwords

Change Screen Colors

Configure the System Date

Configure the System Time

Change the Command Window Title

Chapter 21: Creating and Testing Batch Files

Use Batch File Commands

Test Batch Files

Store and Retrieve Directories with the PushD and PopD Commands

Part VIII: Creating Scripts

Chapter 22: Discovering Scripting Basics

Use Scripting Languages

Execute Scripts

Chapter 23: Using the Scripting Objects

Use the WScript Object

Use the WScript.WshArguments Object

Use the WScript.WshShell Object

Use the WScript.WshNetwork Object

Create a Basic Script

Chapter 24: Creating Advanced Scripting Examples

Script Registry Entries

Script Networking Solutions

Impersonate a User

Change the Environment

Map a Network Drive

Create a .CSV File

Appendix 1

Appendix 2

Appendix 3

Always Verify the Data

Real Administrators Use Help

Test Your Theories on a Test System

Use Batch Files, Scripts, and Written Procedures

Make Backups

Perform User-Specific Changes during Downtime

Index

title page

Acquisitions Editor: Agatha Kim

Development Editor: Jennifer Leland

Technical Editor: Russ Mullen

Production Editor: Liz Britten, Eric Charbonneau

Copy Editor: Cheryl Hauser

Editorial Manager: Pete Gaughan

Production Manager: Tim Tate

Vice President and Executive Group Publisher: Richard Swadley

Vice President and Publisher: Neil Edde

Book Designer: Maureen Forys, Happenstance Type-O-Rama

Compositor: JoAnn Kolonick, Happenstance Type-O-Rama

Proofreader: Jen Larsen, Word One

Indexer: Ted Laux

Project Coordinator, Cover: Lynsey Stanford

Cover Designer: Ryan Sneed

Cover Image: iStockphoto

Dear Reader,

Thank you for choosing Windows Command Line Administration Instant Reference. This book is part of a family of premium-quality Sybex books, all of which are written by outstanding authors who combine practical experience with a gift for teaching.

Sybex was founded in 1976. More than 30 years later, we’re still committed to producing consistently exceptional books. With each of our titles, we’re working hard to set a new standard for the industry. From the paper we print on, to the authors we work with, our goal is to bring you the best books available.

I hope you see all that reflected in these pages. I’d be very interested to hear your comments and get your feedback on how we’re doing. Feel free to let me know what you think about this or any other Sybex book by sending me an email at nedde@wiley.com. If you think you’ve found a technical error in this book, please visit http://sybex.custhelp.com. Customer feedback is critical to our efforts at Sybex.

Best regards,

Edde_sig.tif

Neil Edde Vice President and Publisher Sybex, an Imprint of Wiley

This book is dedicated to Claudia Meyer, who has taught me a great deal about human spirit and the ability to come back after a crisis. She has been an inspiration to me.

Acknowledgments

Thanks to my wife, Rebecca, for working with me to get this book completed. I really don’t know what I would have done without her help in researching and compiling some of the information that appears in this book. She also did a fine job of proofreading my rough draft and page proofing the result. Rebecca also keeps the house running while I’m buried in work.

Russ Mullen deserves thanks for his technical edit of this book. He greatly added to the accuracy and depth of the material you see here. Russ is always providing me with great URLs for new products and ideas. However, it’s the testing Russ does that helps most. He’s the sanity check for my work. Russ also has different computer equipment from mine, so he’s able to point out flaws that I might not otherwise notice.

Matt Wagner, my agent, deserves credit for helping me get the contract in the first place and taking care of all the details that most authors don’t really consider. I always appreciate his assistance. It’s good to know that someone wants to help.

A number of people read all or part of this book to help me refine the approach, test the coding examples, and generally provide input that all readers wish they could have. These unpaid volunteers helped in ways too numerous to mention here. I especially appreciate the efforts of Eva Beattie, Jonathan S. Weissman, and Osvaldo Téllez Almirall who provided general input, read the entire book, and selflessly devoted themselves to this project.

Finally, I would like to thank Pete Gaughan, Agatha Kim, Jennifer Leland, Liz Britten, Cheryl Hauser, and the rest of the editorial and production staff at Sybex for their assistance in bringing this book to print. It’s always nice to work with such a great group of professionals and I very much appreciate the friendship we’ve built over the few years.

About the Author

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 86 books and over 300 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Windows command line reference, books on VBA and Visio 2007, a C# design and development manual, and an IronPython programmer’s guide. His technical editing skills have helped over 52 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He’s also contributed articles to magazines like DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at

http://www.amazon.com/gp/blog/id/AQOA2QP4X1YWP

When John isn’t working at the computer, you can find him in his workshop. He’s an avid woodworker and candle maker. On any given afternoon, you can find him working at a lathe or putting the finishing touches on a bookcase. He also likes making glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at JMueller@mwt.net. John is also setting up a Web site at http://www.mwt.net/~jmueller/. Feel free to look and make suggestions on how he can improve it. One of his current projects is creating book FAQ sheets that should help you find the book information you need much faster.

Introduction

Let’s face it, while GUI applications are nice and they do provide significant levels of hand holding, they’re cumbersome and inefficient. The command line is an essential part of the administrator experience—at least, if the administrator wants to go home at night and spend weekends somewhere other than work. Using the command line can often provide faster results with far less effort. In addition, the command line lends itself to easy automation, so you might not need to manually perform some tasks at all; you can let the computer do them while you have a cup of coffee and gab with a friend in the next cubicle. However, no one can memorize every command, or even a significant subset of them, so Windows Command-Line Administration Instant Reference provides you with a quick reference for the common commands and provides examples of their use to help you avoid potential errors. In short, if you want to spend time doing something other than adding users to the server, you need this book!

Work Faster and More Efficiently

Have you looked at the Administrative Tools folder of the Control Panel lately? It typically contains fourteen or more links to consoles that you use to administer Windows using a GUI. Finding the right console isn’t always straightforward. For example, most people would be tempted to look for hardware settings in System Configuration, but they really appear in Computer Management. Some of the consoles don’t even appear in the Administrative Tools folder. A typical Windows setup includes 21 consoles, so one-third of the consoles are missing—you need to know they exist in order to use them. If you want to set a group policy, you need to know that you have to use GPEdit.MSC (the Group Policy Editor) to change them, but don’t count on Windows helping you. In short, the GUI is disorganized and difficult to use. On the other hand, if you want to use the command line, you open one item—the command prompt. How much simpler can things get?

Speaking of group policies, it requires a single command at the command line to change any group policy. Yes, changing the policy requires that you create the command, but there is help to do that. Changing a policy such as a Domain Profile for Windows Firewall using Group Policy Editor, however, requires that you dig down six levels, figure out which of the policies will actually do what you want, and then perform upward of five additional steps in order to change the policy. Now, imagine that you have to change a lot of policies on a number of machines. When working with the command line, you can create a batch file that performs the task on every machine on your network. The batch file will likely require an hour or so to create and test, while using the GUI may very well require days because the Group Policy Editor lacks automation.

The command line is great for more than just changing settings. Have you ever wanted to discover all of the files associated with a particular application and been frustrated in your efforts? The command line supports a nifty utility called FindStr that lets you look inside files for copyright statements and other strings. If you have some idea of what to look for, FindStr will faithfully search every file on the hard drive for that string—no matter where the file might appear. You can’t even perform this task using the GUI because Microsoft thinks it knows better than you how to find things. The GUI doesn’t even index the inside of every file, just those that Microsoft thinks you should search. In short, if you want to find anything anywhere, you need the command line, not a GUI.

It would be easy to go on and on about the benefits of the command line, but you have an entire book to convince you of the incredible value of using the command line in place of the GUI in most situations. Of course, every good thing comes with caveats and the command line is no different. The command line does require that you be able to type commands accurately in order to obtain accurate results. That’s why you need Windows Command-Line Administration Instant Reference. This book uses a task-based approach to make it easy for you to type just the command you need. It’s packed with hundreds of example commands, some of which will meet your needs with little or no change. If you want to get more out of your day, you need to use the command line with Windows Command-Line Administration Instant Reference.

Goals for Writing This Book

I want you to be able to get started using the command line today to perform useful work. Starting with the first chapter you’ll find that you can immediately begin using the command line to work faster and more efficiently. Suddenly, the system that felt so slow just a few minutes earlier will accomplish work quickly because you’ll take command using a time proven interface. The first and most important goal of this book is to get you to work now.

Most people don’t realize the immense number of commands and utilities available at the command line. In fact, there are commands and utilities to perform some extremely esoteric tasks and you won’t see them covered in this book. Instead, this book exposes you to the commands and utilities that you’ll commonly use; 149 of them. That’s right! You have access to 149 commands and utilities that can make it faster and more efficient to administer the systems under your control. Some of these commands and utilities, such as Windows Management Interface Command line (WMIC) are so powerful that you might decide never to use the GUI again. The second goal for this book is to expose you to the most useful commands and utilities that the command line has to offer. Look at Appendices A and B to see lists of these commands and utilities.

Automation is one of the major benefits of working at the command line. If you need to perform a particular task often, there isn’t any reason to keep doing it manually when you can tell the computer to perform the task for you instead. Using a GUI makes many people think that it’s normal to manually perform the same repetitious work over and over again until your eyes glaze over and you fall asleep out of sheer boredom. Work shouldn’t be boring and the automation that the command line can provide relieves you of boredom. Many administrators have the mistaken idea that they have to become programmers to use automation, but this book shows you that you can create perfectly acceptable batch files to automate tasks without becoming even close to a programmer. So the third major goal of this book is to help you understand how automation comes into play using batch files.

Of course, you may find that batch files are limited and after working with batch files for a while, you might decide that a little programming isn’t so bad after all. The fourth goal of this book is to demonstrate that scripting is not only powerful, but that it isn’t very hard either. Imagine how nice it would be to get all of your work done in half or even a quarter of the time you do now. Working at the command line and using the full scope of automation it can provide will help you move toward that goal. So, the final goal for this book is to help you understand the benefits of scripting. You won’t actually learn how to script using this book—that’s the topic of another tome, but you’ll get enough information to decide whether scripting is right for you. In addition, you’ll see some pretty nifty scripts that will definitely save you time.

Who Should Read This Book?

This book is designed to meet the needs of administrators who are tired of letting the GUI slow them down and who want the productivity advantage of working at the command line. More and more administrators are moving to the command line because it lets them perform tasks quickly. In addition, the command line lends itself to automation, so that you don’t spend hours performing precisely the same boring task over and over again.

Power users will also appreciate this book because it gives them the edge needed to work quickly in today’s competitive market. There isn’t any reason to spend hours mousing about when you can complete the task in a matter of seconds at the command line. For that matter, by using some of the scheduling techniques found in this book, you can automate some tasks completely so that you won’t actually perform them manually again. This book is all about letting the computer do as much for you as it possibly can so you can spend more time watching the game, reading a book, or doing something else that’s a bit more interesting than sitting in front of the computer.

I’m assuming that you already know how to use Windows quite well and that you’ve performed administration tasks in the past. This book presents you with instructions for performing common tasks at the command line and the information you need to customize these tasks to meet your specific needs. However, the complete novice won’t be able to keep up with the pace of this book.

What You Need to Use This Book

You’ll very likely want to set up a test machine when working through the examples in this book. A production machine simply isn’t the right environment in which to learn how to work at the command line. I used Windows 7 and Windows Server 2008 while writing this book. If you have some other version of Windows, you may find that your system doesn’t have some of the features I discuss. You’ll still find plenty to interest you.

I recommend that you set up the hardware required for a two-machine network as part of your test setup. Otherwise, you really won’t see how some of the client-side tasks are supposed to work. Using a virtual machine setup may work acceptably, but I opted for using two physical machines so that the interactions would work just as they do on your network. There are advantages to using two physical machines, because virtual machines sometimes lack the differences in environment that two physical machines provide.

The book contains descriptions of numerous utilities that you can download and use free for the most part. You may prefer other utilities, but I used these utilities to create the content for the book. If you find a technique useful, download the utility that goes with it and try it too. I’m always on the lookout for new utilities, so please be sure to tell me about your utilities at JMueller@mwt.net.

Conventions Used in This Book

It always helps to know what the special text means in a book. The following table provides a list of standard usage conventions. These conventions make it easier for you to understand what a particular text element means.

Table 1-1: Standard Usage Conventions

Convention Explanation
Inline Code Some code will appear in the text of the book to help explain application functionality. The code appears in a special font that makes it easy to see. This monospaced font also makes the code easier to read.
Inline Variable As with source code, variable source code information that appears inline will also appear in a special font that makes it stand out from the rest of the text. When you see monospaced text in an italic typeface, you can be sure it’s a variable of some type. Replace this variable with a specific value. The text will always provide examples of specific values that you might use.
In some cases, a command or utility provides an optional entry. The code form of this entry appears in square brackets. Both code entries and variables can be optional. Whenever you see the square brackets, remember that you have a choice to make about typing the code or variables within the square brackets.
User Input Sometimes I’ll ask you to type something. For example, you might need to type a particular value into the field of a dialog box. This special font helps you see what you need to type.
Filename
A variable name is a value that you need to replace with something else. For example, you might need to provide the name of your server as part of a command line argument. Because I don’t know the name of your server, I’ll provide a variable name instead. The variable name you’ll see usually provides a clue as to what kind of information you need to supply. In this case, you’ll need to provide a filename. Although the book doesn’t provide examples of every variable that you might encounter, it does provide enough so that you know how to use them with a particular command.
[Filename] When you see square brackets around a value, switch, or command, it means that this is an optional component. You don’t have to include it as part of the command line or dialog field unless you want the additional functionality that the value, switch, or command provides.
File Open Menus and the selections on them appear with a special menu arrow symbol. “File Open” means “Access the File menu and choose Open.”
italic You’ll normally see words in italic if they have special meaning or if this is the first use of the term and the text provides a definition for it. Always pay special attention to words in italic because they’re unique in some way.
Monospace Some words appear in a monospaced font because they’re easier to see or require emphasis of some type. For example, all filenames in the book appear in a monospaced font to make them easier to read.
URLs URLs will normally appear in a monospaced font so that you can see them with greater ease. The URLs in this book provide sources of additional information designed to improve your development experience. URLs often provide sources of interesting information as well.
This is the code continuation arrow. It tells you when a single line of code in a file actually appears on multiple lines in the book. You don’t type the code continuation arrow when you use the code from the book in your own code. Rather, you continue typing the code in the book on a single line in your code. For example, you would type the following code on a single line, even though it appears on multiple lines here.
<add connectionString=↵    "Server=MAINVISTA\SQLEXPRESS;↵
   Database=ReportServer$SQLExpress;↵
   Integrated Security=true" ↵
   name="MySQLConnection" />

The book also uses a number of special text entries. These special entries provide you with additional information about the task at hand. The following list describes each of these special text entries.

NOTE Notes provide general ancillary information that doesn’t fit within the confines of the text. For example, a note might tell you about a Web site where you can find additional information. A note can also provide amplifying information, such as the use of a special technique with a particular command or utility. Generally, you can ignore notes if you want, but reading the notes will definitely help you get more out of the book.

WARNING Warnings are especially important to read because they tell you about things that can go wrong when you use a particular command or utility. In fact, not reading warnings can be harmful to your data (and then your career). This book uses warnings to emphasize text—a warning says “Watch out! Danger ahead!” Always read the warnings in the book before you use the associated command or utility. In addition, if you don’t feel you fully understand the warning, be sure you do some additional research before you use the command or utility. Feel free to contact me at JMueller@mwt.net about any warnings you don’t understand.

Interesting Side Topics

You’ll see a number of fascinating side topics in the book. Sometimes these sidebars are appealing to read on their own. You may have a few minutes to kill, which is a good time to thumb through the book looking for these interesting tidbits. Sidebars always contain helpful and pertinent information. This is extra information that’s designed to make your use of the command line better in some way, but you can just as easily skip the information if you want. Treat sidebars as you would articles that you read online. They’ll always provide something of importance, but not always a topic that’s essential for performance of the task at hand.

Part I: Command Line Basics

In This Part

CHAPTER 1: Configuring the Local Machine

CHAPTER 2: Making Remote Connections

CHAPTER 3: Automating Tasks