How good are Chat GPT generated Powershell scripts?

How good are Chat GPT generated Powershell scripts?

Chat GPT is an open-source language-generating AI model developed by OpenAI. It’s latest releases generated massive waves of innovation, concern and awareness across multiple industries about AI. Some fear that it will replace people, while, others grasp its capabilities to enhance their own skills.

I have written several scripts that joined computers to domains or Azure AD, and using that experience, I decided to judge Chat GPT’s Powershell scripting capabilities, error prediction and suggestions.

The first query I made to Chat GPT was “join a computer to azure AD”, and it returned the manual method advertised by Microsoft, and some notes about the licenses and Windows version. It works, but it’s not scalable because it takes too long to execute because its necessary to go through the whole OOBE (out of box experience) and create a local account.

Since it didn’t suggest any automation or script, that can be executed either before or after the OOBE, I asked Chat GPT to generate a Powershell script to join a computer to an Azure AD domain. I started with the following query “Powershell script to join a computer to Azure AD and Wipe the computer with a reset”, but after noticing that it didn’t verify if the module was installed, I changed the query to “Powershell script check if the Azure AD module is installed, then try to to join a computer to Azure AD and Wipe the computer with a reset”.


The Powershell scripts it outputted were well commented and rather well written, but some manual labor would be required to mix and match the best parts of both. For instance, that in one it would like to store the account password in plain text or that it wouldn’t check if the necessary modules were installed.

Query 1

# Azure AD Join and Computer Reset Script
# Define Azure AD credentials

$AzureADUsername = "user@contoso.com"
$AzureADPassword = "password"

# Join the computer to Azure AD

$JoinCommand = "Add-AzureADDeviceRegisteredOwner -DeviceName $($env:COMPUTERNAME) -UserPrincipalName $AzureADUsername -Credential (New-Object System.Management.Automation.PSCredential -ArgumentList $AzureADUsername, (ConvertTo-SecureString $AzureADPassword -AsPlainText -Force))"
Invoke-Expression $JoinCommand

# Perform a reset to wipe the computer

$ResetCommand = "Reset-Computer -Confirm:$false -Credential (New-Object System.Management.Automation.PSCredential -ArgumentList $AzureADUsername, (ConvertTo-SecureString $AzureADPassword -AsPlainText -Force)) -WhatIf"
Invoke-Expression $ResetCommand

Query 2

# Check if the Azure AD module is installed

if (-not (Get-Module -Name AzureAD)) {
    Write-Host "Azure AD module is not installed."
    Write-Host "Please install the AzureAD module before running this script."
    Exit
}

# Prompt for Azure AD credentials

$credential = Get-Credential -Message "Enter your Azure AD credentials"

# Join the computer to Azure AD

Connect-AzureAD -Credential $credential
$deviceName = $env:COMPUTERNAME
Add-AzureADDeviceRegisteredOwner -ObjectId (Get-AzureADUser -SearchString $credential.UserName).ObjectId -RefObjectId (Get-AzureADDevice -SearchString $deviceName).ObjectId

# Wipe the computer with a reset
Reset-Computer -Confirm:$false -Force

My personal conclusion is that the output is as good as the queries and the available documentation it searches through. Since the queries can be edited its possible to hone in the fine details by adding more information to the initial one, after testing and doing some parallel research. Definitely knowing or having some degree of experience in the subject matter of the query produces better results, making me believe that in the future AI may generate specialized roles such as AI operator, QA or moderator in different areas.