Published

AI

AI-Written PowerShell? Test It Safely With -WhatIf First

AI writes PowerShell quickly, but one wrong path can delete or change the wrong files. The -WhatIf switch shows exactly what a command would do, without doing it.

1. Ask for -WhatIf in your prompt

Tell the AI to add -WhatIf to every command that changes, moves or removes something, and to explain each line.

Write a PowerShell script that deletes .log files older than 30 days in C:\Temp. Add -WhatIf to every command that changes files and explain each line.

2. Run the preview

-WhatIf prints What if: Performing the operation … for each item and changes nothing.

Get-ChildItem C:\Temp\*.log | Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) | Remove-Item -WhatIf

3. Read the output

Check every path in the What if lines. Wrong folder, too many files or anything outside C:\Temp means stop and fix the script.

4. Run it for real

Only when the preview looks right, run it again without -WhatIf. Use -Confirm instead if you want to approve each item.

Get-ChildItem C:\Temp\*.log | Where-Object LastWriteTime -lt (Get-Date).AddDays(-30) | Remove-Item

FAQ

Does every PowerShell command support -WhatIf?

Cmdlets that change things usually do (Remove-Item, Set-Item, Stop-Service, Move-Item and many more). Run Get-Help CommandName -Parameter WhatIf to check.

Is it safe to run AI scripts as administrator?

Only if you understand every line. Run as a normal user whenever possible, and never paste a script you cannot read.