Home > Store

Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools

Register your product to gain access to bonus material or receive a coupon.

Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools

Book

  • Sorry, this book is no longer in print.
Not for Sale

Description

  • Copyright 2011
  • Dimensions: 7" x 9-1/8"
  • Pages: 840
  • Edition: 1st
  • Book
  • ISBN-10: 0-7897-3728-0
  • ISBN-13: 978-0-7897-3728-1

THE ONLY HANDS-ON, UP-TO-DATE GUIDE TO VBSCRIPT, THE WINDOWS COMMAND LINE, AND WINDOWS POWERSHELL

Windows 7 and Vista contain state-of-the-art tools for streamlining or automating virtually any system management task. If you’re a power user, administrator, or developer, these tools can help you eliminate repetitive work and manage your systems far more reliably and effectively.

Renowned Windows expert Brian Knittel brings together the practical knowledge you need to use all these tools, including VBScript and Windows Scripting Host (WSH), traditional batch files, the advanced PowerShell command console, and more. Using plenty of examples, Knittel explains how each tool works, and how to solve real-world problems with them.

You’ll master techniques ranging from accessing files to manipulating the Registry, sending automated emails to configuring new users. Knittel also provides concise, handy references to Windows 7/Vista’s command line, GUI scripting, and object-based management tools.

The only single-source guide to all leading methods of Windows scripting and automation, this book will help you get far more done–in far less time!

  •    Understand Windows Scripting Host (WSH) and the modern Windows scripting environment
  •    Script objects with VBScript, JScript, ActivePerl, and ActivePython
  •    Read and write files, including XML and HTML files
  •    Manipulate programs and shortcuts
  •    Manage network, printer, and fax connections
  •    Make the most of PowerShell under Windows 7 and Vista
  •    Monitor and administer Windows systems with Windows Management Interface (WMI)
  •    Use ADSI to control Active Directory and Microsoft Exchange, and manage users more efficiently
  •    Avoid mistakes that can compromise script security
  •    Use Windows’ debugging tools to test and troubleshoot scripts
  •    Develop batch files that take full advantage of the command line
  •    Send faxes and email messages from scripts with Windows Fax and Collaboration Data Objects (CDO)
  •    Deploy your scripts throughout your organization

Brian Knittel has been a software developer for more than 30 years. He has coauthored five
titles in Que’s Special Edition Using series, covering Microsoft Windows Vista, XP, and 2000.
He is also author of Windows XP Under the Hood, and coauthor of Upgrading and Repairing Windows (with Scott Mueller).

Sample Content

Online Sample Chapter

Windows 7 Scripting and Objects

Sample Pages

Download the sample pages (includes Chapter 3 and Index)

Table of Contents

Introduction 1

I Scripting with Windows Script Host

1 Windows Script Host 9

    What Is a Windows Script? 9

        The “Script” Part 9

        The “Windows” Part 10

        The “Host” Part 11

        How Is This Different from Writing Batch Files? 13

    Scripting Languages 13

        VBScript 14

        JScript 14

        Perl 15

        Python 15

        Open Object REXX 15

        Ruby 15

        Choosing a Language 16

    A Simple Script 16

    Types of Script Files 19

        JSE and VBE: Encoded Scripts 20

        Windows Script Files (WSF) 21

        Windows Script Components (WSC) 23

        WSH Settings 23

    Creating Your First Script File 24

        Making and Securing a Script Folder 24

        Creating a Script 26

        Script Editing Tools 27

    How Windows Runs Scripts 28

        Wscript and Cscript 28

        Ways to Run a Script 29

        Passing Information to Scripts 31

        Saving the Results from Scripts 32

        Wscript and Cscript Command Options 33

    Running Your Own Scripts 36

        Adding Scripts to the Path 37

        Running Scripts with a Shortcut Icon 38

        Making a Script Shortcut 39

        Running Scripts from Batch Files 39

        Running Scripts Automatically 40

    Security Concerns 40

        Trust Policy and Script Signing 42

    Debugging Scripts 42

    Where to Get More Information 47

2 VBScript Tutorial 49

    Introduction to VBScript 49

        Variables 50

        Constants 51

        Named Constants 52

        Operators and Expressions 53

        Automatic Conversion 57

    Flow Control 57

        The If…Then Statement 58

        The Select Case Statement 61

        The Do While Loop 63

        Terminating a Loop with Exit Do 65

        Counting with the For…Next Statement 66

        Processing Collections and Arrays with For…Each 67

    VBScript Functions 68

        Calling Functions and Subroutines 69

        Documentation and Syntax 70

        String-Manipulation Functions 71

        Date and Time Functions 74

    Interacting with the User 79

        The MsgBox( ) Function 79

        The InputBox( ) Function 82

        Printing Simple Text Messages with Wscript.Echo 84

    Advanced VBScript Topics 85

        Error Handling 86

        Procedures: Functions and Subroutines 87

        Arrays 89

        Variable Scope 91

    Where to Go from Here 92

3 Scripting and Objects 93

    Introduction to Objects 93

        Classes and Instances 94

        Containers and Collections 95

        Object Naming 97

    Using Objects with VBScript 98

        Automation and Document Files 99

        The Difference Between Properties and Methods 100

        Nested Objects 101

        Releasing Objects 102

        Working with Collections 102

    Using Objects with JScript 104

        Case Sensitivity 104

        Working with Collections 104

    Using Objects with ActivePerl 106

        Running Perl Scripts in WSH 106

        The Perl Object Interface 107

        Working with Collections 108

    Using Objects with ActivePython 109

        Working with Collections 110

    Using the WScript Object 111

        Retrieving Command-Line Arguments 113

    Locating and Using Unusual Objects 115

4 File and Registry Access 123

    Getting Real Work Done 123

    Manipulating Files and Folders 124

        Scripting.FileSystemObject 124

        Working with File and Pathnames 130

        The Scripting.Drive Object 135

        The Scripting.Folder Object 139

        The Scripting.File Object 144

    Reading and Writing Files 149

        The TextStream Object 150

        Reading Text from Files 152

        Writing Text to Files 154

        Working with Stdin and Stdout 159

        Reading Binary Files 163

    Reading and Writing XML 167

        Some XML Basics 168

        Reading an XML File 176

        Creating an XML or HTML File 179

    Manipulating Programs and Shortcuts 181

        The WScript.Shell Object 182

        Running Programs 186

        Creating and Modifying Shortcuts 193

    Working with the Environment 196

        Extracting Environment Information 198

        Managing Environment Settings 199

    Working with the Registry 201

        Examining Registry Keys and Values 202

        Saving Information in the Registry 203

5 Network and Printer Objects 207

    Managing Network and Printer Connections 207

    Retrieving Network User Information 212

    Managing Drive Mappings 214

        Listing Drive Mappings with EnumNetworkDrives 214

        Adding Drive Mappings 218

        Deleting Drive Mappings 219

        Setting Up Mappings in a Script 220

    Managing Network Printer Connections 221

        Displaying Printer Information 222

        Connecting to Network Printers 223

        Redirecting DOS Session Printers 225

        Deleting Printer Connections 226

        Setting the Default Printer 228

    Printing from Scripts 229

6 Messaging and Faxing Objects 231

    Sending Email from Scripts with CDO 231

    The CDO Object Model 232

        The CDO.Message Object 235

        Working with Fields 242

        Fields for the CDO.Message Object 244

        The CDO BodyParts Collection 246

        The CDO BodyPart Object 247

        The ADO Stream Object 250

        The CDO.Configuration Object 250

    Sending a Message with CDO 256

        Constructing the Message 257

        Adding Attachments 261

        Including Images with an HTML Message 262

        Specifying the Recipients and Subject 263

        Specifying the Delivery Server 263

        Sending the Message 265

        Putting It All Together 265

    Faxing from Scripts 271

        Sending a Fax with a Script 274

        Getting More Information About Faxing 277

7 Windows Management Instrumentation 279

    Introduction to Windows Management Instrumentation 279

        WMI Functions 280

        Namespaces 281

        Managing Windows Remotely 283

    Making WMI Connections 287

        WMI Object Hierarchy 288

        Connecting with the WbemScripting.SWbemLocator Object 291

        Connecting with a Moniker 292

        Connecting to the Local Computer 294

        Security and Authentication 294

        Specifying Security Options 299

    WMI Collections and Queries 301

        SWbemServices 302

        WQL Queries 303

        SWbemObject 306

        SWbemMethodSet and SWbemPropertySet 307

    Scriptomatic 310

    WMI Examples 312

        Collecting System Information 312

        Managing Printers 313

        Monitoring Windows Service Packs and Hotfixes 313

        Managing Services and Tasks 315

    For More Information 317

8 Active Directory Scripting Interface 319

    Managing the User Directory 319

        Uses of the Active Directory Scripting Interface 320

        Limitations of ADSI with Windows Script Host 321

    ADSI Concepts 322

        Multiple Inheritance 324

        Creating ADSI Objects 325

        Directory Security 328

        Determining the Difference Between Containers and Leaves 330

    ADSI Objects for the WinNT: Provider 332

        IADs 333

        IADsCollection and IADsContainer 336

        Working with ADSI Collections 339

        IADsComputer and IADsComputerOperations 340

        IADsDomain 342

        IADsFileService and IADsFileServiceOperations 345

        IADsFileShare 347

        IADsGroup 349

        IADsMembers 350

        IADsNamespaces 351

        IADsPrintJob and IADsPrintJobOperations 351

        IADsPrintQueue and IADsPrintQueueOperations 354

        IADsService and IADsServiceOperations 357

        IADsSession 361

        IADsUser 362

    IIS and Exchange 364

    Managing Active Directory 364

        X.500 and LDAP Terminology 364

    Active Directory Objects 368

        RootDSE 368

        IADsO and IADsOU 369

    Developing ADSI Scripts 370

    EzAD Scriptomatic 372

    For More Information 373

9 Deploying Scripts for Computer and Network Management 375

    Using Scripts in the Real World 375

        Designing Scripts for Other Users 376

    Using WSF Files 377

        WSF File Format Reference 379

        Providing Online Help with WSF Files 384

        Processing Command-Line Arguments 386

        Enclosing More Than One Script 390

        Putting It All Together 390

    Deploying Scripts on a Network 394

    Creating Simple Installation Programs with IExpress 395

        Creating IExpress Install Scripts or Batch Files 398

        Dealing with User Account Control 400

        Providing an Uninstall Option 402

    Writing Scripts to Manage Other Computers 403

        Remote Scripting 405

        Replicating Scripts to Multiple Computers 406

    Scripting Security Issues 408

        Script Signing 409

        The Script Encoder 415

    Setting Up Logon Scripts 416

        User Profile Logon Scripts 416

        Scripts for Logon, Logoff, and Other Events on Windows 7 and Vista 418

        Group Policy Logon, Logoff, Startup, and Shutdown Scripts 418

    Scheduling Scripts to Run Automatically 421

        Writing Unattended Scripts 421

        Sending Messages to the Event Log 423

        Scheduling Scripts with the Task Scheduler 428

II The Command Line Environment

10 The CMD Command-Line 433

    The Command Prompt 433

        CMD Versus COMMAND 434

    Running CMD 435

        Opening a Command Prompt Window with Administrator

        Privileges 436

        CMD Options 437

        Disabling Command Extensions 439

    Command-Line Processing 439

        Stopping Runaway Programs 440

        Console Program Input and Output 441

        Using the Console Window 442

        I/O Redirection and Pipes 443

        Copy and Paste in Command Prompt Windows 447

        Command Editing and the History List 448

        Name Completion 450

        Enabling Directory Name Completion 451

        Multiple Commands on One Line 452

        Grouping Commands with Parentheses 453

        Arguments, Commas, and Quotes 454

        Escaping Special Characters 454

    Configuring the CMD Program 455

        AutoRun 455

        Environment Variable Substitution 456

        The Search Path 456

        Predefined and Virtual Environment Variables 459

        Setting Default Environment Variables 461

    Built-in Commands 462

        Extended Commands 475

        Listing Files with the Dir Command 476

        Setting Variables with the Set Command 480

        Conditional Processing with the if Command 482

        Scanning for Files with the for Command 483

    Getting More Information 488

    11 Batch Files for Fun and Profit 491

    Why Batch Files? 491

    Creating and Using Batch Files 492

    Batch File Programming 494

    Displaying Information in Batch Files 495

    Argument Substitution 496

    Argument Editing 498

    Conditional Processing with If 499

        The Basic If Command 499

        Checking for Files and Folders 500

        Checking the Success of a Program 500

        Performing Several Commands After If 501

        Extended Testing 503

    Processing Multiple Arguments 503

    Working with Environment Variables 506

        Environment Variable Editing 507

    Processing Multiple Items with the for Command 508

        Using Multiple Commands in a for Loop 510

        Delayed Expansion 511

    Using Batch File Subroutines 513

    Prompting for Input 514

    Useful Batch File Techniques 515

        Processing Command-Line Options 515

        Managing Network Mappings 518

        Checking for Correct Arguments 519

        Keeping Log Files 519

12 The MS-DOS Environment Under Windows 521

    MS-DOS Programs on Windows 521

        The Virtual DOS Machine 522

        MS-DOS and COMMAND.COM 524

    Configuring the MS-DOS Environment 525

        Window and Memory Options 526

        CONFIG.NT 532

        AUTOEXEC.NT 535

        MS-DOS Environment Variables 536

    MS-DOS and Networking 536

    Printing from MS-DOS 537

        Print Redirection 538

        Print Screen 538

    Configuring Serial Communications with MS-DOS 539

    Using Special-Purpose Devices for MS-DOS 539

    Managing MS-DOS Programs 540

        When Things Go Awry 540

13 Command-Line Utilities 543

    Windows Command-Line Programs 543

    The Essential Command Line 544

    GUI Shortcuts 545

    General-Purpose Shell Programs 547

        findstr 547

        more 552

        tree 553

        xcopy 554

    File-Management Tools 557

        attrib 557

        cacls 559

    Management Power Tools 563

        driverquery 564

        runas 565

        tasklist 565

        taskkill 568

        sc 569

    Networking Utilities 571

        ipconfig 571

        net 574

        netstat 584

        nslookup 586

        ping 589

        tracert 591

    Getting More Utilities 592

III Introduction to Windows PowerShell

14 Windows PowerShell 593

    Introduction to Windows PowerShell 593

        An Object-Oriented Command Shell 593

        Based on the .NET Framework 596

        An Extensible Environment 597

    Obtaining Windows PowerShell 598

    The PowerShell Environment 600

    The PowerShell Command Prompt 601

        Command-Line Editing 602

        Copying and Pasting 603

        Pausing Output and Stopping a Runaway Program 604

        Command-Line Syntax 604

    Cmdlets and Objects and Scripts, Oh My! 607

    Getting Help 610

    Prompting to Complete Commands 612

    Aliases 612

        How to Get a Listing of Aliases 612

        How to Define a New Alias 613

    Navigating Directories and Other Locations 613

    PowerShell Security 615

        PowerShell Scripts and User Account Control 615

        Script Execution Policy 616

    PowerShell Profiles 617

15 PowerShell Programming 621

    The Windows PowerShell Programming Language 621

    Windows PowerShell Syntax 622

    Comments 622

    Variables and Types 623

        Literal Values 625

        Object Methods and Properties 626

        Object Constructors 627

        String Interpolation 628

        Special Characters 629

        Here-Strings 629

        Releasing Variables 630

        Predefined Variables 630

        Arrays 632

        Constants 637

    Expressions 638

        Comparisons with Arrays 640

        String Operators 643

        The & (Execute) Operator 646

        Operator Precedence 646

        Assignment Operators 647

        Statement Values 648

        Casts 649

        Passing by Reference 650

        Hash Tables 650

    Flow of Control 653

        if 653

        while 654

        do…while and do…until 654

        for 655

        foreach 656

        switch 657

        break 660

        continue 661

        Program Blocks 661

    Exception Handling 662

        trap 662

        try/catch/finally 663

        throw 664

    Defining Functions 664

        Function Parameters 665

        Function Scope 668

        The Dot-Source Operator 668

        Variable Scope 669

        Pipeline Functions and Filters 671

        Splatting 672

    Using the .NET API 673

        Calling Static Member Functions 673

        Working with Strings 674

        Working with Dates and Times 676

        Converting Values 680

        Mathematical Functions 680

16 Using PowerShell 683

    Real-World PowerShell 683

    Command-Line Techniques 685

        Generating Objects 685

        Filtering 686

        Taking Actions 689

    Formatting Cmdlet Output 690

        The -f Operator 690

    Working with Files and Folders 691

        Seeing Whether a File Exists 697

        Reading Text from Files 697

        Writing Text to Files 698

        Identifying Files by Size 698

    Creating Useful Scripts 699

        Comment Your Work! 700

        Command-Line Processing 700

        Writing Modules 701

        Exception Handling as an Exit Strategy 702

    Using Hash Tables 703

    The PowerShell Integrated Scripting Environment 704

        Starting the PowerShell ISE 705

        Configuring the ISE 706

        Creating and Editing Scripts 707

        Running Scripts in the ISE 708

        Setting Breakpoints and Single-Stepping 709

        Interactively Examining and Changing Variables 710

        Conditional Breakpoints 711

    Remote and Background PowerShell 712

    Where to Go from Here 712

IV Appendices

A VBScript Reference 713

B CMD and Batch File Language Reference 725

C Command Line Program Reference 735

D Index of Patterns and Sample Scripts 747

E Automation Object Reference 1 (Online)

F WSF and WSC File Format Reference 1 (Online)

G Creating Your Own Scriptable Objects 1 (Online)

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020