Real PowerShell script examples you can safely generate with AI — file cleanup, backups, reporting, Active Directory, Azure automation, and more.

💡 PowerShell Script Examples (AI-Generated): Real Scripts You Can Generate Today

Practical, copy‑ready examples you can generate, customize, and run safely — all using AI as your PowerShell co‑pilot.

This page is your pattern library.
Each example includes:

  • A plain‑English prompt you can paste into Claude, Copilot, or ChatGPT
  • A description of what the script does
  • A safety reminder
  • Links to deeper guides if you want to go further

If you’re new to AI‑First scripting, start with:
👉 AI PowerShell Script Generator
👉 AI for PowerShell Scripting (Tools & Comparison)


⚠️ Before you run anything:
Every prompt on this page is a starting point, not a finished script.
Always request a Dry Run first, ask AI to explain the script back to you in plain English, and never run a script in a production environment without testing it safely first.
New to this? Start with the Safety Layer before using any example below.


Jump to a Section


🧹 1. File Cleanup Examples

These are the most common “Busy Human” automations — organizing, sorting, and cleaning up your digital life.

A. Sort Files by Type (Downloads Cleanup)

What it does:
Moves PDFs → Docs folder, images → Images folder, creates folders if missing.

Prompt to paste:

“I’m on Windows using PowerShell 7.
I want a script that:

  • Looks in [PATH]
  • Moves .pdf files to a Docs subfolder
  • Moves .jpg and .png files to an Images subfolder
  • Creates folders if they don’t exist
  • Only affects files directly in this folder, not subfolders

Add:

  • Dry Run mode using -WhatIf
  • Logging to a .txt file
  • Comments explaining each step.”

👉 See also: Sorter Pattern

B. Delete Old Files (Log or Temp Cleanup)

What it does:
Finds files older than X days and deletes them safely.

⚠️ High risk if misused. Always run in Dry Run mode first and confirm the file list before allowing any deletions. Never point this script at a folder you haven’t verified manually.

Prompt:

“Write a PowerShell script that deletes files older than [X] days in [PATH].
Include:

  • A Dry Run mode using -WhatIf that shows exactly what would be deleted — run this first and require explicit re-confirmation before proceeding to actual deletion
  • Only affect files directly in this folder — do not recurse into subfolders unless I explicitly add a -Recurse flag
  • Logging of every deleted file with timestamps
  • A confirmation prompt that displays the file count and total size before deleting anything
  • Comments for each major step.”

C. Archive Screenshots Automatically

What it does:
Moves screenshots older than 30 days into an Archive folder.

Prompt:

“I want a script that moves .png files older than 30 days from [PATH] into an Archive subfolder.
Add Dry Run, logging, and comments.”

 


📁 2. Folder & Backup Examples

A. Daily Backup of Important Files

What it does:
Copies changed files to a backup folder while preserving structure.

Prompt:

“Write a PowerShell script that copies files changed in the last [X] days from [SOURCE] to [DESTINATION], preserving folder structure.
Add Dry Run, logging, and clear variables at the top.”

B. Sync Two Folders (One‑Way Mirror)

What it does:
Copies new or updated files from Folder A to Folder B. Does not delete anything from either folder.

⚠️ “Sync” can mean different things to different AI tools. Some interpret it as a true mirror, which deletes files from the destination that aren’t in the source. The prompt below explicitly prevents this — do not remove that instruction.

Prompt:

“Write a PowerShell script that copies new or updated files from [SOURCE] to [DESTINATION] (one‑way).

  • Only copy files that are new or have been modified more recently in the source
  • Do not delete any files from the destination folder under any circumstances — not even if they no longer exist in the source
  • Add Dry Run mode using -WhatIf
  • Add logging
  • Explain how to run it safely.”

 


📊 3. Reporting & Analysis Examples

A. Generate a CSV Report of Files

What it does:
Scans a folder and outputs a CSV with name, size, and last modified.

Prompt:

“Write a PowerShell script that scans [PATH] and outputs a CSV with:

  • File name
  • Size
  • Last modified date

Add comments and explain how to open the CSV in Excel.”

👉 Related: Quick‑Learn Excel

B. Find Large Files (Disk Space Audit)

What it does:
Lists the largest files in a folder or drive.

Prompt:

“Write a PowerShell script that finds the largest [N] files in [PATH] and outputs a sorted report.
Include comments and a CSV export option.”

C. Count File Types (Folder Inventory)

What it does:
Counts how many PDFs, images, videos, etc. are in a folder.

Prompt:

“Write a PowerShell script that counts file types in [PATH] and outputs a table of extensions and counts.
Add comments and a CSV export option.”

 


🧑‍💻 4. Windows System Automation Examples

A. List Installed Applications

What it does:
Creates a CSV of installed programs — great for audits.

Prompt:

“Write a PowerShell script that lists all installed applications with name, version, and install date.
Output to CSV. Add comments.”

B. Monitor a Folder for New Files

What it does:
Watches a folder and logs new files as they appear.

📝 Note: This script uses FileSystemWatcher, which can miss events if a large number of files arrive at once. It’s best suited for low-to-medium volume folders. Ask your AI to note this limitation in the script comments.

Prompt:

“Write a PowerShell script that monitors [PATH] for new files and logs each new file with a timestamp.

  • Use FileSystemWatcher and include a comment explaining its limitations for high-volume folders
  • Add comments and explain how to stop the script safely.”

📝 Tip: This is great for automating your daily triage — pair it with 👉 The AI Daily Routine to decide when these checks should run.

C. Restart a Service If It Stops

What it does:
Checks a Windows service and restarts it if needed.

⚠️ Production risk. Restarting the wrong service — or restarting a service at the wrong time — can cause outages. Only use this on non-critical services and always test outside production first.

Prompt:

“Write a PowerShell script that monitors the [SERVICE NAME] service and restarts it if it stops.

  • Include a hardcoded safelist containing only [SERVICE NAME] — the script should refuse to act on any service not in this list
  • Check every 5 minutes
  • Log every restart attempt with a timestamp and the result
  • Add a comment warning that this should only be used in non-production environments without further review
  • Add comments throughout.”

 


🧩 5. Active Directory Examples (Safe & Beginner‑Friendly)

💡 Architect’s Note:
For Active Directory scripts, ensure your AI includes the correct authentication step:
Import-Module ActiveDirectory

⚠️ AD scripts carry real organisational risk. Always run in Dry Run mode first, test in a non-production or lab environment, and have an AD administrator review the output before running in production. Mistakes here can affect real user accounts across your organisation.

A. Create Users from a CSV

What it does:
Reads a CSV and creates AD users with OU placement — as disabled accounts requiring a separate enable step.

Prompt:

“Write a PowerShell script that reads a CSV with columns Name, Department, and Email, and creates AD users in OUs based on Department.

  • Create all accounts as disabled by default — do not enable them in this script
  • Do not set, store, log, or echo passwords anywhere in the script — use a separate secure password reset process
  • Add Dry Run mode that shows exactly what accounts would be created without making any changes
  • Add comments explaining each step.”

B. Find Inactive Accounts

What it does:
Lists AD accounts not used in X days — read-only, no changes made.

Prompt:

“Write a PowerShell script that finds AD accounts inactive for [X] days and outputs a CSV.
No destructive actions. Add comments.”

C. Reset Passwords in Bulk (Safe Mode)

What it does:
Resets passwords for a list of users from a CSV — with confirmation and no password logging.

⚠️ Sensitive operation. Review the target user list carefully before running. Ensure passwords are never logged, stored, or echoed anywhere in the script or its output.

Prompt:

“Write a PowerShell script that resets passwords for users listed in [CSV PATH].

  • Do not log, store, echo, or display the new passwords anywhere — not in the console, not in log files, not in comments
  • Require a -Force flag to proceed with any changes
  • Display the list of affected accounts and require explicit confirmation before making any changes
  • Log only the username and timestamp of each reset — never the password
  • Add comments throughout.”

 


☁️ 6. Azure & Cloud Examples

💡 Architect’s Note:
For Azure scripts, ensure your AI includes the correct authentication step:
Connect-AzAccount or Connect-MgGraph

A. Find Untagged Azure Resources

What it does:
Lists all untagged resources for cost control — read-only, no changes made.

Prompt:

“Write a PowerShell script using the Az module that finds all Azure resources missing tags and outputs a CSV.
Add comments and explain how to authenticate.”

B. Export Azure VM Inventory

What it does:
Creates a CSV of all VMs with size, region, and status — read-only, no changes made.

Prompt:

“Write a PowerShell script that exports all Azure VMs with name, size, region, and power state.
Output to CSV. Add comments.”

 


🛡️ Safety First (Always)

Before running any script:

  • Use Dry Run / -WhatIf and review the output carefully before proceeding
  • Ask AI to explain the script back to you in plain English — if the explanation doesn’t match your intent, fix the prompt and regenerate
  • Ask for a security audit using the Architect’s Audit Prompt
  • Test outside production first — never run an untested script against live systems, user accounts, or cloud resources
  • Check the AI Security Watchlist for active threats
  • Never run destructive commands without confirmation

For deeper safety guidance:
👉 AI PowerShell Script Generator (safety layer)


🧭 Next Steps


🏠 Home ← Back to AI for Coding
🆘 Need help getting AI to do what you want? Start with Help! I’m Stuck