Windows Command-Line: The Personal Trainer Windows 8.1, Windows Server 2012 & Windows Server 2012 R2 William Stanek PUBLISHED BY Stanek & Associates PO Box 362 East Olympia, WA 98540-0362 Copyright © 2015 William Stanek All rights reserved. No part of the contents of this book may be reproduced, stored or transmitted in any form or by any means without the express written permission of the publisher. United States of America The Personal Trainer and Stanek & Associates are trademarks of William Stanek. All other marks are property of their respective owners. The example names for companies, organizations, persons and other named elements used in the book are fictitious. No association with any real company, organization, person or other named element is intended or should be inferred. This book expresses the author’s views and opinions. The information contained herein is provided without any express, statutory or implied warranties. Neither the authors, the publishers, the resellers nor distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Cover Design: Creative Designs Ltd. Editorial Development: Andover Publishing Solutions Technical Review: L & L Technical Content Services You can provide feedback related to this book by emailing the author at [email protected]. Please use the name of the book as the subject line. Contents at a Glance Introduction ...................................................................................................................................... 16 Chapter 1. Overview of the Windows Command Line .............................................................. 20 Chapter 2. Getting the Most from the Command Line ............................................................. 37 Chapter 3. Command-Line Scripting Essentials ......................................................................... 52 Chapter 4. Working with the Registry ......................................................................................... 94 Chapter 5. Managing System Services ........................................................................................ 109 Chapter 6. Restarting and Shutting Down Computers from the Command Line ............... 120 Chapter 7. Event Logging, Tracking, and Monitoring ............................................................. 129 Chapter 8. Managing Processes and Maintaining Performance .............................................. 156 Chapter 9. Scheduling Tasks to Run Automatically ................................................................ 193 Chapter 10. Configuring, Maintaining, and Troubleshooting TCP/IP Networking ......... 233 Appendix A. Essential Command-Line Tools Reference ........................................................ 295 Table of Contents Introduction ...................................................................................................................................... 16 What’s This Book About? .............................................................................................................. 16 What Do I Need to Know? ............................................................................................................ 16 How Is This Book Organized? ...................................................................................................... 17 What Conventions Are Used in This Book? ............................................................................... 17 Other Resources ............................................................................................................................... 17 Chapter 1. Overview of the Windows Command Line .............................................................. 20 Command Line Essentials ............................................................................................................... 21 Understanding the Windows Command Shell ....................................................................... 21 Understanding Windows PowerShell ...................................................................................... 27 Configuring Command-Line Properties ................................................................................... 29 Working with the Command History ...................................................................................... 31 Making Supplemental Components Available ............................................................................ 33 Using the Microsoft Remote Server Administration Tools for Windows 8.1 ................... 33 Registering the Remote Server Administration Tools Package ............................................ 34 Configuring and Selecting Remote Server Administration Tools ........................................ 35 Removing the Remote Server Administration Tools ............................................................. 35 Removing the Remote Server Administration Tools Package .............................................. 36 Chapter 2. Getting the Most from the Command Line ............................................................. 37 Managing Command Shell Startup ............................................................................................... 38 Working with the Command Path ................................................................................................ 41 Managing the Command Path ................................................................................................... 41 Managing File Extensions and File Associations ..................................................................... 43 Redirecting Standard Input, Output, and Error ......................................................................... 44 Redirecting Standard Output to Other Commands ............................................................... 46 Redirecting I/O to and from Files ............................................................................................. 47 8 Redirecting Standard Error ........................................................................................................ 48 Chaining and Grouping Commands ............................................................................................. 48 Using Chains of Commands ....................................................................................................... 50 Grouping Command Sequences ................................................................................................ 50 Chapter 3. Command-Line Scripting Essentials ......................................................................... 52 Creating Command-Line Scripts ................................................................................................... 52 Common Statements and Commands for Scripts ....................................................................... 54 Clearing the Command-Shell Window .................................................................................... 55 Adding Comments to Scripts ..................................................................................................... 56 Managing Text Display and Command Echoing .................................................................... 57 Fine-Tuning Command Echo with @ ...................................................................................... 58 Setting the Console Window Title and Colors ....................................................................... 59 Passing Arguments to Scripts......................................................................................................... 62 Getting Acquainted with Variables ............................................................................................... 63 Using Variables in Scripts ............................................................................................................... 65 Naming Variables ........................................................................................................................ 65 Setting Variable Values .............................................................................................................. 66 Substituting Variable Values ..................................................................................................... 68 Localizing Variable Scope ........................................................................................................... 70 Using Mathematical Expressions ................................................................................................... 71 Working with Arithmetic and Assignment Operators .......................................................... 72 Understanding Operator Precedence ........................................................................................ 73 Simulating Exponents ................................................................................................................. 74 Command-Line Selection Statements .......................................................................................... 74 Using If .......................................................................................................................................... 75 Using If Not .................................................................................................................................. 76 Using If Defined and If Not Defined ........................................................................................ 77 Contents | 9 Nesting Ifs ..................................................................................................................................... 77 Making Comparisons in If Statements ..................................................................................... 78 Command Line Iteration Statements ............................................................................................ 79 Iteration Essentials ....................................................................................................................... 79 Stepping Through a Series of Values ........................................................................................ 81 Iterating Through Groups of Files ............................................................................................ 82 Iterating Through Directories ................................................................................................... 84 Parsing File Content and Output .............................................................................................. 86 Creating Subroutines and Procedures .......................................................................................... 89 Using Subroutines ....................................................................................................................... 90 Using Procedures ......................................................................................................................... 92 Chapter 4. Working with the Registry ......................................................................................... 94 Understanding Registry Keys and Values .................................................................................... 94 Querying Registry Values ............................................................................................................... 97 Comparing Registry Keys ............................................................................................................... 98 Saving and Restoring Registry Keys ............................................................................................ 100 Adding Registry Keys .................................................................................................................... 101 Copying Registry Keys .................................................................................................................. 102 Deleting Registry Keys .................................................................................................................. 103 Exporting and Importing Registry Keys ..................................................................................... 104 Loading and Unloading Registry Keys ........................................................................................ 105 Chapter 5. Managing System Services ........................................................................................ 109 Viewing Configured Services........................................................................................................ 109 Starting, Stopping, and Pausing Services.................................................................................... 112 Configuring Service Startup ......................................................................................................... 113 Configuring Service Logon ........................................................................................................... 114 Configuring Service Recovery ....................................................................................................... 116 10 | Contents