Using ChatGPT as a PowerShell Debugging Tool
ChatGPT has evolved to effectively debug PowerShell scripts. Learn about its ability to identify and correct errors.
June 4, 2024
![yellow block stuck in between blue cog wheels yellow block stuck in between blue cog wheels](https://eu-images.contentstack.com/v3/assets/blt07f68461ccd75245/blt55a17485c0208ef1/665dde5d6fa786a948832121/image-1200x675.jpg?width=850&auto=webp&quality=95&format=jpg&disable=upscale)
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.
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.
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)
You May Also Like