Home › IT ops & DevOps › Write a shell/PowerShell script: how to do it with AI

Write a shell/PowerShell script: how to do it with AI

Last checked

From official vendor pages; selected tasks also include dated logged-out free-plan trials. Report an error.

Beginner Risk: Medium 🛠️ IT ops & DevOps

An AI assistant can turn a plain description like "disable accounts that haven't signed in for 90 days" into a PowerShell or Bash script and explain each line. It saves time on scripts you write once a month and forget how to write.

Who it’s for: IT admins, helpdesk staff and developers who run occasional admin scripts.

Why medium risk: Scripts run with your permissions; a wrong filter can change or delete many accounts or files at once. Scripts that change Active Directory or Entra ID are High risk.

Best pick

ChatGPT has the highest score on our published rubric (94/100) of the 4 tools listed for this task. Free plan has unlimited text chats, enough for drafting and explaining scripts. Codex is included in limited form on Free.

Step by step

  1. Write down the goal, the platform (Windows PowerShell 5.1, PowerShell 7, Bash) and the exact modules you have, such as ActiveDirectory or Microsoft.Graph.
  2. Ask for a read-only version first that only lists what it would touch.
  3. Run the read-only version and check the list is what you expect.
  4. Ask for the change version with -WhatIf (PowerShell) or a dry-run flag, plus logging to a CSV.
  5. Run it with -WhatIf in a test VM, then on a small pilot group.
  6. Only then run it for real, and keep the log.

AI prompts to write a shell or PowerShell script

Replace the [brackets] with your details. See all prompt examples

Read-only first
Write a PowerShell 7 script using the [module] module that LISTS (does not change) [objects] where [condition]. Output name, last change date and the reason it matched to a CSV. Explain each line in a comment.
Add a safe change step
Now add the change: [action]. Support -WhatIf and -Confirm, stop on the first error, and write every change to a log file. List separately every line that modifies or deletes something.
Explain before I run
Explain this script line by line for someone new to PowerShell. Flag anything that changes, deletes or needs admin rights, and anything that would behave differently on a large directory:

[paste script]

Illustrative example

You paste

Goal: list local user accounts on this Windows server that are disabled.
Platform: PowerShell 5.1, read-only.

A good answer looks like

Get-LocalUser | Where-Object { -not $_.Enabled } |
  Select-Object Name, LastLogon |
  Export-Csv .\disabled-users.csv -NoTypeInformation
# Read-only: lists accounts, changes nothing.

Illustrative example written for this guide, not output captured from a specific tool.

Safe practice

We tried it

Logged-out free-plan trial on . Each card lists the model or plan shown during the run. Not tested yet: Claude (incl. Claude Code), GitHub Copilot, Cursor.

Prompt used

Write a PowerShell 7 script using the Microsoft.PowerShell.Management module that LISTS (does not change) local user accounts where Enabled -eq $true. Output name, last change date and the reason it matched to a CSV. Explain each line in a comment.

Google Gemini Model/plan: Free (logged out) · Tested: · ~15s · Free

Output (trimmed)

Here is a PowerShell 7 script that queries local user accounts, filters for those where Enabled -eq $true, and exports the name, last change date (PasswordLastSet), and match reason to a CSV file. Every single line of code is explained with an inline comment: PowerShell # Define the destination file path and name for the output CSV report $csvPath = "C:\temp\EnabledLocalUsers.csv" # Retrieve all local user accounts existing on the local machine $localUsers = Get-LocalUser # Filter the collection of local users to keep only accounts where Enable …

Produced a commented list-only script targeting enabled local users. Confirm the exact property names on your PowerShell 7 build; module assumptions can differ by OS.

ChatGPT (incl. Work & Codex) Model/plan: Free (logged out) · Tested: · ~12s · Free

Output (trimmed)

Below is a PowerShell 7 script that only reads local user accounts and exports accounts where Enabled -eq $true to a CSV file. It includes the account name, last password-change date, and the reason the account matched the filter. Important: Microsoft.PowerShell.Management provides Get-LocalUser on supported Windows systems, but Get-LocalUser is implemented by the Microsoft.PowerShell.LocalAccounts module. The script imports that module to retrieve local account information. It does not enable, disable, create, or modify any accounts. …

Returned a read-only PowerShell 7 script with comments and CSV export. Still verify Get-LocalUser fields and -WhatIf habits on a non-prod machine before you trust it.

Not tested yet: Claude (incl. Claude Code), GitHub Copilot, Cursor.

Which tool to pick, and why

Tool notes below come from official plan and feature pages. The “We tried it” section above is separate hands-on output. Sources: claude.com/pricing · chatgpt.com/pricing · github.com/features/copilot/plans · cursor.com/pricing

Common mistakes

Privacy tip

Replace real server names, domains and usernames with placeholders like SERVER01 or [email protected] before pasting. In ChatGPT, turn off "Improve the model for everyone" in Data controls; in Cursor, turn on Privacy Mode.

ChatGPT (incl. Work & Codex) setting · Cursor setting · Claude (incl. Claude Code) setting · Sources: help.openai.com/en/articles/7730893-data-controls-in-chatgpt · cursor.com/help/security-and-privacy/privacy · privacy.claude.com/en/articles/12109829-how-do-i-change-my-model-improvement-privacy-settings

Related tasks

Report an error on this page