r/PowerShell • u/Bloaf • Jul 12 '26
Misc I love watching AI use powershell
At work I've got access to several latest-and-greatest AI models and harnesses, I also run windows. That means lots of the tasks they do for me end up as powershell commands/scripts.
Watching them develop, I see them repeatedly step on the same language land-mines that I do whenever I try to write powershell by hand. Things like && only existing in powershell 7, powershell auto-unwrapping single-element lists, and quote-escaping rules are reliable sources of failure for both me and the AI.
Its quite vindicating seeing that even SOTA programming-oriented LLMs struggle with the same language "features" that I do. For a long time I've heard the powershell community saying things like "you just need to understand that its an object oriented language" whenever people complain about footguns like these, but along comes an AI that can do PhD level work in multiple programming domains, and even its advanced understanding of object orientation hasn't saved it.
155
u/BetrayedMilk Jul 12 '26
AI makes the same mistakes as the people it trained off of. Wow!
4
u/Idakay Jul 12 '26
Every environment has a few niche environmental variables that are useful to load up in your system prompt or insurrection file.. (we don't have ps 7 here so write for 5.1, winrm is disabled in environment so blah blah blah). You'll see a lot of those failures dissipate if you load the model up with the info they need to succeed before you've got it writing code :)
25
u/1RedOne Jul 12 '26
No LLM understands how powershell will throw when you dereference a missing property
24
u/justaguyonthebus Jul 12 '26
Add the Microsoft Learn MCP server and tell it to reference that for all powershell development.
One of the big issues is that AI (and humans) tries to apply generalized development techniques to PowerShell and it intentionally took a different direction. It's designed for the system admin learning to automate and not for the developer.
6
u/jstenoien Jul 12 '26
That's a whole lot of words to tell everyone you don't understand how AI works :)
7
u/endowdly_deux_over Jul 12 '26
LLMs have no understanding. They just regurgitate what they were trained with at an incredible degree of precision with impressive math. It explains its mistakes a lot better than a “lack of understanding”. GIGO.
8
u/Aggressive_Apple3774 Jul 12 '26
This resonates. I ended up building a tool around exactly this problem, so disclosure up front: I maintain [claude-powershell-lsp](https://github.com/manderse21/claude-powershell-lsp), a Claude Code plugin, and this comment is partly about it.
The framing that helped me: you cannot stop a model from emitting these landmines, because it is trained on oceans of bash and C#-flavored code and the failure modes are baked in. What you can do is close the loop -- run real analysis on every edit and feed the diagnostics straight back into the model's context, so it fixes the mistake before you ever run the script.
Someone in the thread suggested pointing the AI at the Microsoft Learn MCP server, and I think that is a genuinely good idea -- but it is the other half of the same coin. Docs in context are prevention: give the model the right idioms up front. Static analysis in the loop is verification: check what it actually wrote. In my experience prevention alone does not hold, which is basically the OP's observation -- the model's bash and general-purpose priors leak through even when the right docs are sitting in context. You want both.
That commenter's deeper point -- PowerShell intentionally went a different direction, and AI keeps applying generalized dev habits to it -- is also why a bare "rule violated" message is not enough. If the model's training says Write-Host is fine, a naked rule name will not always change its mind. So each finding my plugin surfaces carries a short "why" line explaining the PowerShell reasoning, with hand-tuned explanations for exactly the rules where general dev instinct and PowerShell design disagree (Write-Host vs the output stream, the ShouldProcess pattern). The model correcting itself works a lot better when it is told why the idiom exists.
On the OP's specific examples, honest scorecard:
- && on 5.1: covered directly. An AST pass flags PS7-only syntax (&& / ||, ternary, ?? / ??=, ?.) unless the file declares #Requires -Version 7. It also flags bash-isms (grep, sed, awk, export, chmod, etc.) for when the model forgets which shell it is writing for entirely.
- Quote-escaping: partially. There is no quoting-style lint, but botched quoting usually surfaces as a parse error, and the hook reports parse errors plus full PSScriptAnalyzer output immediately after each edit, so the agent self-corrects in the same turn instead of you finding it at runtime.
- Single-element unwrapping: not covered, and I will not pretend otherwise. It is a runtime semantic, and any static rule for it would false-positive constantly. Defensive @() wrapping is still the answer there.
Scope caveats so this is not a sales pitch: it is Claude Code specific, and if you are already in VS Code with the PowerShell extension, Claude Code's IDE integration hands you PSScriptAnalyzer diagnostics for free -- you do not need my plugin. Where it earns its keep is terminal, SSH, CI, and headless setups where no editor is supplying diagnostics.
And yes -- it is genuinely vindicating watching a frontier model hit the exact same unwrapping surprise I have been hitting for a decade. Object orientation was never the problem.
4
u/NEBook_Worm Jul 12 '26
My favorite is when AI tells you the best way to get a list of installed programs is to query Win32_Product...
4
u/Il_Conte_ Jul 12 '26
Been coding in PS for 15 years, I have yet to see a script made by AI that requires zero corrections.
15
u/vermyx Jul 12 '26
You're saying that the model was trained on bad coding standards. When you don't properly curate your AI model you get harbage in garbage out. That's what I am reading.
1
u/FinsToTheLeftTO Jul 12 '26
I’ve told Copilot to always flag code that only works in either PS 5 or 7, it still messes up along with the constant trailing back ticks, improper API versions, and munged HTML.
3
u/Fallingdamage Jul 12 '26
Yeah. At work we are given claude for teams to use as a tool. 9 out of 10 requests dont give me functional code. Its ok for snippets, but when I ask it for anything fully functional it falls off a cliff, but only after burning more tokens than a kid at a nickel arcade.
4
u/buffychrome Jul 12 '26
Not to be contrarian, but I’ve used Opus 4.8 heavily this past week to write and modify several complicated scripts, scripts using classes, enums, dynamic parameters, running passes over existing scripts to transition them to PS7, another few scripts that leaned on ascii escape sequences and cursor manipulation. I only had to correct Claude a handful of times, or make corrections.
However, I’ve got 15 years of Powershell scripts, around 510 altogether, that Claude Code was working from for reference and context. Also, I have a very explicit scripting style: I type-cast and scope variables as much as possible, make regular and heavy use of direct .Net classes and methods, and never use positional parameters as a hard rule. I lean heavy into the verbosity of Powershell by using variable names that are self-documenting and semantic.
What I’m saying is, my explicit scripting style that leaves little room for ambiguous interpretation may be one of the reasons I haven’t seen many issues with AI and Powershell.
1
u/lotekjunky Jul 12 '26
you should make a set of custom instructions and list these so you never have to see them again
1
u/MyNameIsNotMud Jul 12 '26
When I use AI to program PowerShell to query databases, for instance, I make it a point to tell the AI to convert all data structures to native PowerShell structures.
That has far lessened the tripping up that AI does with PowerShell
1
u/hoshisabi Jul 12 '26
I tried putting in Claude MD that it was in Windows and it had to be careful about some of these issues, but it actually made it seem worse. It runs bash to run powershell most of the time, and hits issues, and then other times runs pwsh directly and hits issues the other way.
1
u/screamtracker Jul 13 '26
I need it to stop giving me strings with :
Looks real fancy until it breaks every time 😂
Already know how to fix, it's just funny that it chooses violence
1
u/Due-Consequence9579 Jul 14 '26
I hate the single element list thing. Dumbest god damn behavior.
1
u/HerrBadger Jul 14 '26
Asking AI to use PowerShell is the closest thing I’ve seen to making an application sweat, it’s fantastic
1
u/trepidprism Jul 16 '26
I've created a claud skill for powershell for myself with a bunch of info on when to use pwsh or how I want my pscuatom objects set so at least its only running into those errors once until I remember to update the skill.
1
u/g3n3 Jul 20 '26
Point it at powershelltraps github repo
1
u/RichardMuggedHimself Jul 12 '26
I’ve been using Claude almost every day for about a year at work and it has completely leveled up my powershell game. I can triage issues with servers and databases much faster than before and the syntax is usually perfect. The way I do it is a lot of copy/pasta back and forth and of course we get a complete picture of the environment before we commit any changes and if I am uncomfortable with the potential change then I can get a report of our work up to the commit point and send it out for additional input.
0
u/slackmaster2k Jul 12 '26
For real. Install WSL and let it bash. Lots of what you need will be available and you won’t have to watch it fix commands constantly.
0
u/thehuntzman Jul 12 '26
Put global instructions in to address these problems and force it to always use strict mode and initialize typed variables and you'll waste less tokens fighting these issues. For instance, I have instructions for my agents to never use New-Object and always use [object]::new() since it prefers the older way of doing things presumably because most of its training data is from the original days of powershell where that was the only way.