top of page
  • Writer's pictureJames Agombar

Using Chat GPT for Powershell Scripting

Updated: Dec 28, 2022

What's Chat GPT?

Chat GPT, or Generative Pre-training Transformer, is a powerful tool that can assist in writing PowerShell scripts. PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. It helps system administrators automate tasks and configure systems more efficiently.

Benefits of using Chat GPT for writing PowerShell scripts:

  1. Improved efficiency: Chat GPT can help you write PowerShell scripts faster by suggesting commands and code snippets as you type. This can save you time and effort, especially if you are working on a complex script with multiple commands.

  2. Enhanced accuracy: Chat GPT can help ensure the accuracy of your scripts by suggesting the correct syntax and commands. This can help avoid errors and ensure that your scripts are functional and efficient.

  3. Easy learning: Chat GPT can provide assistance and guidance as you learn PowerShell scripting. If you are new to PowerShell or need to refresh your knowledge, Chat GPT can provide helpful tips and suggestions to help you get up to speed quickly.

  4. Improved collaboration: Chat GPT can facilitate collaboration on PowerShell scripts by allowing multiple users to work on the same script simultaneously. This can help streamline the development process and ensure that everyone is on the same page.

Example of using Chat GPT to create a Powershell Script

Here is a PowerShell script that will report all global administrator accounts with their last logged in time:

# Import the Azure AD module

Import-Module AzureAD

# Connect to Azure AD


# Get all global administrator accounts

$globalAdmins = Get-AzureADGroupMember -ObjectId (Get-AzureADGroup -Filter "displayName eq 'Company Global Admins'").ObjectId

# Loop through each global administrator account

foreach ($globalAdmin in $globalAdmins) {

# Get the user object for the global administrator

$user = Get-AzureADUser -ObjectId $globalAdmin.ObjectId

# Get the user's last login time

$lastLogin = (Get-AzureADUserLogonSession -ObjectId $globalAdmin.ObjectId).LastLogonDateTime

# Output the global administrator's name and last login time

Write-Output "$($user.DisplayName) last logged in at $lastLogin"



Using Chat GPT for writing PowerShell scripts can provide a range of benefits, including improved efficiency, enhanced accuracy, easy learning, and improved collaboration. Whether you are a seasoned PowerShell developer or new to the platform, Chat GPT can help you write scripts more effectively and efficiently.

Disclaimer. This post was entirely written using Chat GPT ;p

102 views0 comments


bottom of page