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
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.
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
- 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.
- Ask for a read-only version first that only lists what it would touch.
- Run the read-only version and check the list is what you expect.
- Ask for the change version with -WhatIf (PowerShell) or a dry-run flag, plus logging to a CSV.
- Run it with -WhatIf in a test VM, then on a small pilot group.
- 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
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.
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 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
- Run anything that changes systems in a test VM or lab tenant first, never straight on production.
- Use a least-privilege account: only the rights the task needs, not Domain Admin or Global Administrator.
- Never paste passwords, API keys, connection strings, tenant IDs with secrets, or client secrets into a chat. Use placeholders like <SECRET>.
- Read every command that modifies or deletes before you run it, and ask the AI to list those lines separately.
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
- Claude (incl. Claude Code): Free plan includes files and code execution, so it can check script logic you paste in. Claude Code (Pro) works in your terminal with permission prompts. Visit official website ↗
- ChatGPT (incl. Work & Codex): Free plan has unlimited text chats, enough for drafting and explaining scripts. Codex is included in limited form on Free. Free plan can do this Visit official website ↗
- GitHub Copilot: Free plan: 2,000 completions a month plus limited chat and agent usage inside your editor, handy if you keep scripts in VS Code. Free plan can do this Visit official website ↗
- Cursor: Its agent can run terminal commands in your editor, so keep it pointed at a test machine. Free plan has a limited allowance. Free plan can do this Visit official website ↗
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
- Running the first draft straight on production.
- Not saying which PowerShell version or module you have, so it uses commands you don't have.
- Pasting real usernames, server names or secrets into the chat.
- Skipping -WhatIf because the script "looks right".
- Letting an editor agent run commands with an admin account.
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