PowerShell and AI: Using ChatGPT with PowerShell to Automate Tasks
Published Feb 23 2023 11:16 AM 48.4K Views

AI is here to help us, but it can be daunting. I wanted to know how effective is it? I wanted to see how I could quickly generate a PowerShell script, how accurate it would be, and then how can I automate this task?


As an ITPro, we perform countless tasks every single day in our jobs. We talk about automation and writing scripts, but it can take time and we always question if it’s worth it. I can write a PowerShell script and automate that script fairly easily, only because I have done this many times. Finding the time to automate our simple and repeatable tasks is the first blocker. Does the time it takes to automate something make sense?


In this blog we are going to look at AI, can it help us be more effective at our jobs? I am going to ask ChatGPT to write a PowerShell script for me and test the script to see if it works. If it does, it still means that I need to run this script at regular intervals. While it may only take 15 minutes out of my day, I want this task to run automatically and save me that 15 minutes so that I can be more effective at other tasks in my job.


We’re going to look at a scenario that many of us can relate to. We’ve deployed resources into Azure, but they are not being maintained properly. Instead of logging into the Azure portal and looking at my resources, trying to figure out what part of the infrastructure belongs to who, or what project. I am going to use a PowerShell script to search for any resources that do not have a tag attached to them.

Why a tag? Let’s say our team is implementing best practices and we require every resource to have tags assigned to them. Each organization will find the tagging strategy that is best for them, but in this case, we require 2 tags on every resource: username of who deployed it, which environment it belongs to (Production, Test, Dev, etc). Tags will allow all of our teams (developers, ITPros, etc) to label their resources, in theory, if something is not tagged then it is not being managed appropriately.


Instead of combing through our Azure portal everyday, manually scrolling for resources without tags, I want to use a PowerShell script to query our resources in a subscription and list out items that do NOT have tags assigned to them.

First, I created a ChatGPT account. From there I started a new conversation and asked ChatGPT:




ChatGPT provided me with a script, but more importantly, it explained what each line of the script was doing. I am learning the components that make up this script. This is where I find AI extremely useful. It has given me a starting point with a script, and then continues to explain to me what role each parameter and command is doing.


ChatGPT outputted the following PowerShell script:




$resources = Get-AzResource

foreach ($resource in $resources) {
    if ($resource.Tags.Count -eq 0) {
        Write-Output "Resource '$($resource.Name)' ($($resource.Id)) does not have any tags."



I copied the script and pasted it into VSCode. Immediately, there were not any linting issues (Lint tools act as astatic code analysis for any errors, bugs, or stylistic errors), so that is a positive start. I executed the script and had this output:




While this does deliver the correct information, I didn’t find it very human friendly. First lesson learned, be specific with ChatGPT. I then went back to ChatGPT and asked more specifically for it to create a PowerShell script and to output it in a table:




The new script then put my output in a more readable table:




The table is very readable, but I realized I was not asking ChatGPT questions with enough specifics to produce the output that was I was used to. Remember, with ChatGPT: When asking it for something, be as specific as possible in what you want in your script. Don’t expect it to read your mind, it cannot do that.


Think of ChatGPT as the new Stack Overflow. Ask it the question that you want to know to gain that output. For fun, I took a known working PowerShell script that I wrote myself and compared how I write my scripts in direct to comparison to what ChatGPT produced (my personal script is on the right): 


 My Personal PowerShell ScriptMy Personal PowerShell Script


ChatGPT's PowerShell ScriptChatGPT's PowerShell Script




We could debate as to which script is better, our technical backgrounds and personal preferences will determine this. I do prefer my script on the right, why? It's simple. I love simple scripts. I feel they are easier to manage, easier to build on top of, and easier to troubleshoot. However, both scripts output the exact same data in a table format. Using ChatGPT allowed me to have a working script in a matter of seconds, and that has a lot of value when I have a lot of tasks in my daily work.




Now that we’ve generated a simple, yet functional PowerShell script. I want to automat this script so that I do not have to spend my time each day running this. Personally, there are 2 methods for automation that I prefer:

  1. GitHub Actions
  2. Azure Functions


Automating your PowerShell Script with GitHub Actions


I love GitHub Actions, because you can automate tasks very easily. In order to build a GitHub Actions Workflow, you do need a GitHub account. In order to create our workflow file, I asked ChatGPT to create one for me, sometimes getting started is the hardest part.




ChatGPT did provide a valid response, but note that it states 'the general steps', so I took that with a grain of salt. What I also got was a GitHub Actions workflow file, written in YAML. If you have never written YAML, this is a great way to get started, use a starter script every time: 



name: Azure Tag Check

    - cron: '0 0 * * *'

    runs-on: ubuntu-latest
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Install Azure CLI
      uses: azure/login@v1

    - name: Install Az PowerShell module
      run: Install-Module -Name Az -Force

    - name: Run PowerShell script
      shell: pwsh
      run: |
        $resources = Get-AzResource
        foreach ($resource in $resources) {
          if ($resource.Tags.Count -eq 0) {
            Write-Output "Resource '$($resource.Name)' ($($resource.Id)) does not have any tags."



As before, ChatGPT provided me with an explanation of how the workflow file is composed and provided directions on how to implement the workflow file.






I ran my workflow from my GitHub repository and it worked on the first run! The workflow is based on a timer trigger, meaning it will run on a regular schedule. I can amend the schedule to best fit my needs. One thing that I did not love about the YAML output, was that ChatGPT puts your script into your workflow file itself. This completely makes sense from a technical point of view, I asked it to build my workflow file using my newly found script. That can get a bit urnIn better working practice, 


I am pretty impressed with the output from ChatGPT. I have a working PowerShell script, a working GitHub Actions Workflow file, and I have gained back 15 minutes of time during my day. All of this was achieved in less than 15 minutes with the assistance of ChatGPT. 


Caveat: You need a GitHub account, and some working knowledge of GitHub of how repositories work. Get started using GitHub by starting with their documentation here. 


The hardest part of learning new technology is getting started and knowing where to start. I found with the use of ChatGPT I could get a base level starting point for some automation without having to write any scripts on my own. This gives me a starting point in which I can further automate other tasks and set them to run on a regular schedule as well. All of those 15 minute tasks during my day have been taken off my shoulders.


Automating PowerShell scripts with Azure Functions


Azure Functions are a fantastic tool that allow you to trigger an automate task between systems. I have used Azure Functions to process files, get data from another system, and to automate tasks. Azure Functions are serverless resources that can be triggered in various ways: HTTP requests, timers, etc. You can read all about Azure Functions here. 


Azure Functions provide a massive benefit that allow you perform transactions without the overhead maintenance of running a virtual machine. Which includes patching and managing the OS, compute under it, security, etc. With Azure Functions, that is all taken care of as it is a PaaS (Platform as a Service) offering in Azure. They are also extremely cost effective.


This was a great opportunity to ask ChatGPT to tell me how to create a function with our new PowerShell script:




ChatGPT providing me with the following directions and also linked me to the correct documentation for further reference. I appreciate that the output was a set of directions and also how to find more answers. It gave me a starting point and a way to grow my knowledge. 


I created a new Function App with a new Function per the directions from ChatGPT. I will say, as you create the Azure Function from within the Azure portal, very carefully look at your configuration options. It had auto selected monitoring with Application Insights for me. While monitoring is important, it does come with a cost, just be aware.




After my function was created with my new script, I ran my new function and it failed. ChatGPT told me to just copy and past my PowerShell script into the Azure Function. While I did follow those steps,  my test run failed. I then spent a few hours troubleshooting my configuration. While ChatGPT made it sound easy, a simple copy/paste, there are some configuration challenges that I hit. To get my Azure Function running, it took more time than I expected. I would 100% recommended using VSCode to create your Azure Function in lieu of the portal. Follow the documentation here.


Azure Functions are powerful and really great to work with, but it will take more than a simple copy/paste to get started. It is worth the investment of your time, but just be aware that it could take more than a cup of coffee to get it up and running


Integrate GitHub Actions into Azure Functions


You can combine the goodness of Azure Functions with the capability to deploy and manage your script with complete automation from your GitHub repository. I found this easier to configure using the ‘Deployment’ tab in the portal:




The Azure Portal directly authenticates to my GitHub repos and I can select my preferred repository. I had already saved my PowerShell scripts into a repository, enabling me to directly hook into this feature.  This would allow for any updates in my script to be kept in a repository, any changes to my script are tracked, as well as allowing my Azure Function to always be working off of my latest script.


Note: You can navigate to the ‘Deployment Center’ within Azure Functions and configure this after you have deployed your Azure Functions App as another option. 


In Summary


I found ChatGPT a great resource to enable me to get started with scripting PowerShell, also helping me to learn a better way to write scripts. For me, it is a great and easy replacement for Stack Overflow. The output for creating a GitHub Actions workflow was much faster and easier to achieve further automation of my script, more quickly. Allowing me to better refine the process once I had something working.


AI is here to enable us, it is in no way perfect, but it can help provide us with a good starting point, references and a baseline in which to work from. I challenge you to create a ChatGPT account and generate some PowerShell scripts.



Version history
Last update:
‎Feb 23 2023 11:16 AM
Updated by: