Script Windows Like A Pro: Command Line, Batch Files, Remote Desktop Connection and pywin32

Here are a few useful corners of the vast array of Windows scripting tools.

Helpful Windows Command Line Documentation

A Few General Windows Commands

Use find to look in a text file to count the lines matching a string:

find /C "FAIL" < "Test_Results.txt"
# returns: 0 if no match or # of lines found, e.g. 2,50,100

I wrote a post on findstr, which offers similar functionality.

clip: pipe commands into the clipboard.

If: If Statements based on if files exist.

List ip address-related info:


Check system bit (usually 64-bit or 32-bit):

wmic os getosarchitecture

Automate Windows Scripts with Batch Files

  1. Save below as a .bat file.
  2. This uses cmd to open a new command prompt in a Windows batch file.
  3. cd into my python virtual env then activate it by running a batch file.
  4. Then call dir to print directory contents.
cmd /k "cd C:\Users\your_username\PythonEnv\Scripts & activate & cd .. & dir"

Set a custom system ‘last_name’ variable to be recalled later.

set /p last_name=Enter a last name:
echo %last_name%

Here we print it out with echo. Then pause.

Line continuation in batch files:

Use ^ to continue your batch file scripts on a new line.

System Assessment Tools: powercfg and sfc

Display system stats:


Use powercfg to assess power, sleep and system states

powercfg /SLEEPSTUDY

Use sfc to perform a system file check:

# scan and repair
# scan, but do not repair:

Accessing a Remote Computer From the Command Line

You may want to ping a remote computer to see if it’s running. Add your ip address instead of the below 1s and 0s:


Log into your Remote Desktop with mstsc:

  1. Run Remote Desktop Connection, save an RDP file from Windows Desktop Client.
  2. You may need to adjust your credentials on your local machine.
  3. Finally, trigger login to an active window from command prompt:
mstsc RDP_File_Name.rdp

WinRM and WinRS can allow terminal access to your Remote Desktop. You may need to set your wifi network to private. To configure winrm:

winrm quickconfig

Log into a remote computer with winrs and run ipconfig:

winrs -r: -t:600 -u:administrator -p:$%fgh7 ipconfig

Check Out Python’s pywin32 Module

This module is extremely useful for scripting out Windows applications. For example, I’ve made good use of its interfaces to Outlook and Task Scheduler. Install with pip:

python -m pip install pywin32

Here’s an example to send an Outlook email:

import win32com.client

outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = ''
mail.CC = ''
mail.Subject = 'Moneyball Review'
mail.Body = """
            Moneyball is a inspiring movie, based on real events.
            Brad Pitt, Jonah Hill and Philip Seymour Hoffmann gave great performances.
            The trade deadline scene is delightful. Wow.
            Chris Pratt as Hatteberg too. What a solid film.
            Money isn't everything. Playing ball is.

One thought on “Script Windows Like A Pro: Command Line, Batch Files, Remote Desktop Connection and pywin32

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.