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:

ipconfig 

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

wmic os getosarchitecture

Automate Windows Scripts with Batch Files

Batch files can be run from command prompt or by double-clicking them. Here’s an example of text in a batch file that activates a python virtual environment. Swap in your username and environment if you’ve created it.

cmd /k "cd C:\Users\your_username\PythonEnv\Scripts & activate & cd .. & dir"
  1. Save above 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.

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

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

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:

systeminfo

Use powercfg to assess power, sleep and system states

powercfg /SLEEPSTUDY

Use sfc to perform a system file check:

# scan and repair
sfc /SCANNOW
# scan, but do not repair:
sfc /VERIFYONLY

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:

ping 01.10.10.01

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:https://myserver.com -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 = 'someone@example.com'
mail.CC = 'name@example.com'
mail.Subject = 'Moneyball Review'
mail.Body = """
            Moneyball is an 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.
            """
mail.Attachments.Add('Baseball_Analysis.csv')
mail.Send()

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

Leave a Reply