Do you know how to write a PowerShell script? If the answer is no, let me introduce you to your new friend OpenAI Chat GPT 3. OpenAI Chat GPT uses a human-spoken natural language to generate your desired code.

So all you need to do is describe what you want your script to do, and it will write the script for you. let’s take a look

What is OpenAI Chat GPT

OpenAI Chat GPT is not only for PowerShell code. GPT-3 (short for “Generative Pretrained Transformer 3”) is a state-of-the-art natural language processing model developed by OpenAI. It is one of the largest and most powerful language models ever created, with 175 billion parameters. GPT-3 is trained on a massive amount of data and is capable of generating human-like text and completing a wide range of natural language tasks. It is the successor to the GPT-2 model, which was also developed by OpenAI “Answered by the AI.”

This means that OpenAI Chat GPT is not only for PowerShell. It’s an AI chat that you can use human-spoken language to interact with. This includes a lot of subjects, not only computer-related chat.

But my focus on this site is PowerShell, so we will set the focus and evaluate the code generated by the AI.

Paid vs. Free ChatGPT features

OpenAI released for ChatGPT two plans, a free and a subscription plan. Both plans provide you with the same experience as AI Chat, but there is a catch.

Free VS Paid

The current Free Plan allows you to access the ChatGPT on low demand, but if the servers are loaded, you won’t be able to log in or do anything. Instead, you need to try again later. Also, the response is slower and you won’t have early access to the new features.

The Paid Plan which costs 20$ per month allows you to access all the features even while the servers are loaded. The response time also is faster and you will get priority access to the new features.

Do you really need to pay 20$?

IMHO, No, if you are using GPT as an assistant and you can wait, no need to subscript as the free plan provides you access to the same features as the paid. But if you are using GPT for application integration, then yes, you better subscript as you don’t need your application to wait or hang for about 2 hours just to be able to connect and get a response from the servers.

Start using OpenAI Chat GPT.

To start using ChatGPT, you can go to https://chat.openai.com/chat. Once you are there, you need to log in. You can use Gmail, your Microsoft account, or your email to create the account.

At the moment of writing this post, the OpenAI GPT 3 is free to use.

Once you log in, you will find a small text box at the bottom of the page, like the one used for regular chat.

GPT3
ChatGPT after login page

Generating PowerShell Script using GPT3

Generating a Simple Hello World Text

Type in the chat box the following

Create a PowerShell Script showing “Hello world, Yes I made it”

I asked ChatGPT

You can make the question as you wish.

The following is a complete response from the AI.

Complete OpenAI ChatGPT response to the question.

This is outstanding. The way it’s explained is fantastic. Anyone can understand it, even people who don’t have much experience in PowerShell.

Let’s make it a bit more complex and request some coloring and formatting for the text.

Generating a Simple Hello World Text with Formating

Send a chat message asking the following.

Create a PowerShell Script showing “Hello world, Yes I made it”. Make the first two words in Red, and the remaining in Yellow

Asked OpenAI GPT

— AI response —

$string = "Hello world, Yes I made it"
$firstTwo = $string.substring(0, 11)
$remaining = $string.substring(11)
$firstTwo | Write-Host -ForegroundColor Red
$remaining | Write-Host -ForegroundColor Yellow

——-End of AI response ——

Running this code will do exactly what the request was about.

Running the OpenAI GPT 3Code

But notice that the Hello World is in a line and the remaining text in a separate line. You can adjust the request by clicking on the Edit icon and adding to use the -NoNewLine parameter.

Editing your sent request

Write the following

Create a PowerShell Script showing “Hello world, Yes I made it”. Make the first two words in Red, and the remaining in Yellow. use -NoNewLine

— AI Response —

Write-Host "Hello world," -ForegroundColor Red -NoNewLine
Write-Host " Yes I made it" -ForegroundColor Yellow

— End of AI —-

Creating Active Directory users using ChatGPT

Let’s make it a bit more realistic and request a PowerShell script to create an AD user, type the following

PowerShell script to create Active Directory user as the following Full name “Faris Malaeb” UPN “f.malaeb” SamAccountName “f.malaeb”

—- AI Response —

$user = New-ADUser -Name "Faris Malaeb" -UserPrincipalName "f.malaeb" -SamAccountName "f.malaeb" -Path "OU=Users,DC=domain,DC=com"
Set-ADUser $user -AccountPassword (ConvertTo-SecureString "password" -AsPlainText -Force) -Enabled $true
Set-ADUser $user -ChangePasswordAtLogon $true
Add-ADGroupMember -Identity "Domain Users" -Members $user

Write-Output "Active Directory user account created for Faris Malaeb with UPN f.malaeb and SamAccountName f.malaeb"

—- End of AI Reponse —–

This is cool, but the AI is returning the code only, and I need more. I need an explanation for the code, so lets request to show the code with an explanation. Ask the following question.

PowerShell script to create Active Directory user as the following Full name “Faris Malaeb” UPN “f.malaeb” SamAccountName “f.malaeb” and explain it for me.

I think the way it’s explained by the AI is better than how I explain it. The sentence is structured and well-written.

———— AI Response ————

Here is a possible PowerShell script that can be used to create an Active Directory user with the specified Full name, UPN, and SamAccountName:

Copy code
# Import the Active Directory module
Import-Module ActiveDirectory

# Define the user's Full name, UPN, and SamAccountName
$FullName = "Faris Malaeb"
$UPN = "f.malaeb"
$SamAccountName = "f.malaeb"

# Create the user object in Active Directory
New-ADUser -Name $FullName -UserPrincipalName $UPN -SamAccountName $SamAccountName
The script first imports the Active Directory module, which provides cmdlets for managing Active Directory objects, such as users and groups.

Next, it defines the Full name, UPN, and SamAccountName for the user. The UPN is the user's login name, which typically takes the form of an email address (e.g. user@domain.com). The SamAccountName is a unique identifier for the user that is used by some older applications.

Finally, the New-ADUser cmdlet is used to create the user object in Active Directory. It takes the Full name, UPN, and SamAccountName as arguments and creates the user with those values.

Note that this script only creates the user object in Active Directory. It does not set a password for the user or add the user to any groups. These additional steps may be needed depending on your specific requirements.

————- End AI Response —–

Using OpenAI ChatGPT to make a RegEx PowerShell Script

One of the complex things in programming lang. in general is Regex. It’s hard to understand the structure. I wonder if ChatGPT can help. Let’s ask OpenAI GPT 3 if it can generate a script to validate email addresses using Regex

Type the following

PowerShell Script to read email address regex

— AI Response —-

$emailRegex = '^[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$'

$input = Read-Host "Enter an email address"

if ($input -match $emailRegex)
{
Write-Host "Valid email address"
}
else
{
Write-Host "Invalid email address"
}

— End AI Response —-

Let’s test the code

Trying the code with a correct email format
Trying the code with an incorrect email format.

Looking for explination, just ask for it, you can adjust the reqeust and add with explination

Conclusion

One final thought about ChatGPT is that it is a powerful and versatile tool for generating human-like text. Its ability to generate coherent and natural-sounding responses to a wide range of inputs makes it a valuable asset in applications such as chatbots, automated customer service, and content creation. While it may have limitations and may require additional training or fine-tuning in certain situations, its potential for creating engaging and lifelike text makes it a promising technology for the future. “Written by AI”

4.9/5 - (12 votes)