Lo-Fi Python

Feb 26, 2023

Using Bing + GPT-4 to Write a Python Script for Windows Computer Maintenance

Today, I was granted access to Bing's large language model, the next generation GPT-4. Bing's chat is the "more powerful" successor to OpenAI's groundbreaking chat service that's generating heaps of hype in addition to its AI text generation abilities. I gained the new chat functionality a few weeks after joining the Bing waitlist.

chatting with Bing

A guilty pleasure of mine is fixing up old Windows machines by installing updates and running command prompt tools like SFC and chkdsk. There's also GUI based tools like disk cleanup and the disk defragmenter.

My HP computer on Windows 10 is running slow and steady. It's an old model, so I want to improve performance wherever possible and hopefully speed it up. Enter Bing. It did what I wanted and more, but I needed to rephrase my first question to get better results.

First, I asked how to improve performance on a Windows computer. Bing offered generic windows maintenance tips, then followed up to ask my operating system version, which is Windows 10. When I informed Bing it offered more targeted advice after I rephrased my question to focus on command line commands.

Bing chat response
Bing chat suggestions

I then asked a new question in a fresh chat session. This time I included my OS and asked more specifically for Windows command prompt commands for improving performance. Bing listed a few options and gave a little context of what they do. Then I was presented options to show how to use powercfg or msconfig.

msconfig examples

How to use powercfg according to Bing:

how to use powercfg

Bing chat helped me find and remember the SFC /scannow command. It caches copies of files, fixes corrupted files and may speed up your machine.

You can even prompt Bing for detailed examples of how to use specific windows commands. To get this kind of result from a search engine is incredible. It's like Bing is your own personal tutor. As someone who is constantly googling how to do computer and programming related things, this feels impressive. This was my first time using this service and it has raised the bar for searching the web. I then prompted Bing for a Python script capable of running the SFC command and had my first "wow" moment.

Bing writing a Python script
Bing writes a Python script

Then it asks if I want the Python code explained. I'm so chuffed at this point. It tapped the subprocess module to write the script and capture its output. Simply stunning.

SFC requires an administrator command prompt. You can start an admin command prompt from the start menu. I ran "python run_sfc.py" containing the below script on version Python 3.11. The command finished running approximately 20 minutes later. Additionally, if you get the error 'utf-8' bytes can't be decoded, you'll need to pass an encoding argument to decode(). Ok, here's the Python script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Import the subprocess module
import subprocess

# Define the SFC command as a list of arguments
sfc_command = ["sfc", "/scannow"]

# Run the SFC command using subprocess.run and capture the output
sfc_output = subprocess.run(sfc_command, capture_output=True)

# Print the output of the SFC command
print(sfc_output.stdout.decode())
Bing Chat coding
Bing chat explains code
Bing chat explains code

If your text is using a non-English character encoding, I found you may need to pass a different encoding besides the default utf-8. For example, I found this to work if your command prompt characters are in Spanish:

1
2
# en espaƱol
print(sfc_output.stdout.decode(encoding="latin-1"))
Python encoding traceback

You could also use a Windows batch file of course:

Windows batch file instructions from Bing