Using ChatGPT as a PowerShell Debugging Tool

ChatGPT has evolved to effectively debug PowerShell scripts. Learn about its ability to identify and correct errors.

Brien Posey

June 4, 2024

4 Min Read
yellow block stuck in between blue cog wheels
Alamy

Since its launch, ChatGPT has demonstrated the ability to write PowerShell code. Initially, the AI-generated code tended to be buggy and unreliable. I often found myself pointing out the errors in the code it generated.

More recently, however, I have noticed an improvement in the quality of ChatGPT’s code. Because ChatGPT seems to have mastered PowerShell, I began to wonder if ChatGPT could be used as a script debugging tool.

ChatGPT’s Debugging Performance

To test this, I started by asking ChatGPT to generate an intentionally buggy PowerShell script that I could use for educational purposes. ChatGPT provided a short script that checks for the existence of certain files and reports their sizes if they exist. Since I didn’t want to ask ChatGPT to debug a script it had created, I made a few changes to the script and introduced some bugs of my own.

You can see what the script looks like in Figure 1. As you review it, you will notice the missing bracket in the Else section and a variable mismatch in the ForEach loop. I created a variable called $FilePaths but referenced it as $Files within the ForEach statement.

Figure 1. This is the buggy script that I decided to use.

I had a few different ideas for using ChatGPT to debug the script. My initial idea was to simply paste the script into ChatGPT and ask it what might be wrong. Although that approach probably would have worked, I came up with a better idea.

Related:How To Build an AI-Driven PowerShell Code Generator (Sample Script)

Because ChatGPT knows PowerShell so well, it is safe to assume it has a deep knowledge of both PowerShell code and PowerShell errors. Like any other AI, ChatGPT thrives on access to verbose information. The more detailed information you give it, the better it performs in most cases. With this in mind, I decided to provide ChatGPT with the script’s code and the error messages it produced.

To accomplish this, I opened PowerShell and used the Type command to display the code within my script. Next, I executed the script and took a screenshot containing both the script’s code and the resulting error. You can see the screenshot in Figure 2.

Figure 2. This is the screenshot I created, showing the script’s source code and the error produced.

Next, I pasted my screenshot into the ChatGPT prompt by pressing Ctrl+V. I then typed, “Here is a screen capture of a PowerShell script and the errors that it has produced. What is wrong with the script?” ChatGPT took several seconds to analyze the screenshot and extract the text within. From there, ChatGPT summarized the errors in the script and generated a corrected version. You can see the results in Figure 3.

Related:Transforming CI/CD Pipelines for AI-Assisted Coding: Strategies and Best Practices

Figure 3. ChatGPT found the errors and created a corrected version of the script.

Testing ChatGPT Beyond Syntax Errors

Given ChatGPT’s obvious proficiency in finding and fixing syntax errors, I began to wonder if it could fix problems where PowerShell scripts don't produce an error but still fail to deliver the intended results. To test this capability, I modified a PowerShell-based calculator app that I wrote last year. The change I made used the same screen position for both the 1 key and the 2 key. This caused the 2 key to be hidden from view, as shown in Figure 4.

Figure 4. I introduced a bug into my calculator script that causes the 2 key to display incorrectly.

At this point, I copied the source code and pasted it into ChatGPT. I then asked it why the button does not display.

I have to admit, ChatGPT blew my mind. Not only did it find the error and provide a correction, but it also found the error almost instantly. ChatGPT answered my question within a second or two of me pressing the Enter key. You can see the response in Figure 5.

Figure 5. ChatGPT identified the error that I introduced.

Based on my testing, ChatGPT does seem to do an excellent job of debugging PowerShell scripts. Like any AI, ChatGPT will make mistakes. However, it performed flawlessly during my debugging tests.

Learn More About PowerShell and AI:

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like