Here are a few useful corners of the vast array of Windows scripting tools.
Helpful Windows Command Line Documentation
- Windows command line Documentation Syntax
- Command-line reference A-Z
- Command shell overview
- Using command redirection operators
- About Windows Remote Management
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
- Save below as a .bat file.
- This uses cmd to open a new command prompt in a Windows batch file.
- cd into my python virtual env then activate it by running a batch file.
- 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% 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
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:
Log into your Remote Desktop with mstsc:
- Run Remote Desktop Connection, save an RDP file from Windows Desktop Client.
- You may need to adjust your credentials on your local machine.
- Finally, trigger login to an active window from command prompt:
WinRM and WinRS can allow terminal access to your Remote Desktop. You may need to set your wifi network to private. To configure winrm:
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
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 = 'firstname.lastname@example.org' mail.CC = 'email@example.com' 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. """ mail.Attachments.Add('Baseball_Analysis.csv') mail.Send()