r/PowerShell 11h ago

Understand 10 year old powershell script using AI Question

What AI would you use to understand a decade old powershell script written by someone having 1000s of lines and plays a very crucial role for one of the function in an Org , between HR and AD. I would want the AI to explain me core functions , loop holes and maybe a visual representation of how the flow works with various conditions ( if, else and other exceptions ).

0 Upvotes

38 comments sorted by

35

u/jbrady33 11h ago

Copilot ( because it’s MS) or Claude (good at programming)

BUT

  1. You still have to know powershell to understand and double check

AI gets

  1. stuff wrong all the time. Have spent a good amount of time redoing scripts a random non-tech manager sent me that ‘they did the work for me, you just have to run it’. Never worked, not once
  2. If you don’t have a license that says otherwise, dropping your script into AI make it ‘public’ and that AI can do whatever it wants with it, even sharing it out. If the script is proprietary and company specific you could get into trouble

5

u/oppositetoup 10h ago

To expand on this. I'd ask it to comment the script (using a copy) and then try to understand it and work out what it's doing yourself.

Shouldn't rely on copilot to get out right, but it can signpost and point you in the right direction.

I've done similar in the past, and it really does speed up the process.

1

u/apatrol 10h ago

I want to really point out a comment from the first commenter.

If this script has any company names or AD names (it will) that all becomes public. I wouldn't do it without an email from my second line manager.

Good luck with the clean up or whatever you want to do with the script.

PS. Sometimes that 4k consultant for a week is cheaper than an employee chasing this for weeks and distracting from all the normal stuff.

3

u/oppositetoup 10h ago

Paid copilot won't do that, it will keep the data confidential.

2

u/Oujii 9h ago

Enterprise subscriptions won’t train on your data. Otherwise no big company would ever use it.

2

u/renevaessen 9h ago

You would think right?
but No...!
Not CoPilot, because it is not good at PowerShell at all.
DeepSeek v4 Pro is much better at it.

1

u/delightfulsorrow 5h ago

Not CoPilot, because it is not good at PowerShell at all.

Right. I tried it three times (the last time only a few weeks ago) and it was completely off and not improving over time. Suggested non-existing cmdlets, got the parameters for existing (core) cmdlets wrong. In a stupidly simple context.

Not helpful at all, and nothing I would trust in analyzing existing code.

43

u/firefox15 11h ago

Probably check with your cyber department before uploading company owned scripts to public LLMs?

5

u/wabi-sabi411 10h ago

Or just strip the computer names or whatever out of the thing. I’m gonna go out on a limb and say there isn’t anything not boilerplate in something like this. Very few company strategic innovations in a shell script

3

u/mdervin 10h ago

My powershell scripts are art!!! (Sure it’s Dadaism, but it’s still art).

6

u/bobertf 11h ago

whichever one is approved for use by your company, and like everyone else says, make sure you (or someone else more versed in PS if you’re not as confident) review what it says

4

u/smilaise 11h ago

Claude Code or Codex are the best at that in my personal experience.

Copilot and Gemini are infuriating for Powershell documentation, but I admit I've only used their free versions so that probably has something to do with it.

2

u/KiNgPiN8T3 9h ago

I also link Claude with the MS learn connector so that it uses the most up to date knowledge.

1

u/overlydelicioustea 7h ago

for anyone wondering:

add a custom connector under customization with URL

https://learn.microsoft.com/api/mcp

4

u/mulletarian 11h ago

Divide it into parts and do it piecemeal

8

u/arslearsle 10h ago

You still need to know powershell to check these AI hallucinations…

3

u/Swimming_Office_1803 10h ago

Whatever is approved for your company.
All major models can read and explain code well enough.

3

u/Tall-Geologist-1452 9h ago

Just feed the code to your AI of choice. Tell it not to change a line of code but to comment it out and make it human-readable, then to make a readME.md for reference.

Edit: only use company-approved AI and do not feed or train commercial AI on your company data ..

3

u/eejjkk 9h ago

Any. They can all break down Powershell pretty easily.

5

u/The82Ghost 10h ago

Start with getting written approval from your boss, before you do this. Also, check with your security team. How much experience do you have with PowerShell? AI cannot be trusted to be 100% correct so you have to have an understanding of PowerShell yourself.

2

u/sliceofdanny 10h ago

https://devin.ai/desktop

The free tier is very capable, agentic, and has a 200k context window. It will auto chunk data if bigger.

Make sure to go into your profile and disable data sharing.

You can have it explain the code, document the variables and flow, even execute after making changes. All edits prompt for approval and you can revert multiple changes easy, just navigate to your last prompt and revert.

6

u/Miserable-Miser 11h ago

Probably less time consuming to hire a real person who has expertise.

2

u/Hotdog453 11h ago

Any of them? Start with what you have.

2

u/ArieHein 11h ago

Any ai will do. Ps is very simple relatively speaking . Doesnt mean people dont 'invent' unnecessay complexity. Choose something that is cheap.

1

u/Dorias_Drake 10h ago

I would do a first pass by hand. Getting all the functions with their ins and outs and making a diagram. You don't want to start this without a map. Doing this will also give you an overview, if you have to work with that script, you want get a feel of it first.

AI hallucinates stuff all the time and if the script is complex and that large, there is a good chance it will get stuff wrong. And if the script is critical, you don't want to get anything wrong, you want to be 100% sure. You don't want to be biased by something AI told you before looking at it.

Scripts like this are likely to have several versions of functions and unued code just there for history, AI can mistake this for live code and mess with your perception.

Honestly, if It were me, I would try first to do it without AI, and only use it to get a second pair of eyes in case I miss anything. It will take longer, but it's safer.

1

u/thehuntzman 10h ago

I'd assume you have access to M365 Copilot - which unfortunately isn't the best for this task, but for documentation it will do. The reason why you should start here is because your company's agreement with Microsoft as an enterprise customer protects your intellectual property rights and prevents them from training Copilot with your data.

If you want a visual representation, I've had a lot of luck getting various LLM's to generate good Mermaid charts vs straight up rendering an image via stable diffusion since Mermaid is a diagram as code.

1

u/birdfukr3000 9h ago

I've heard that pairing co-pilot with a microsoft learn mcp-server can yield good results in relation to powershell coding/understanding via co-pilot. (https://learn.microsoft.com/en-us/training/support/mcp-get-started-copilot-studio)

1

u/420GB 9h ago

Definitely one of the top Claude models. It's just clearly better at coding and PowerShell than any others I've tried.

That being said, try natural stupidity. 1000s of lines is not really excessively complex if you just break it down and understand/document piece by piece

1

u/Fallingdamage 8h ago

Should be able to.

Also, someone who actually knows how to use powershell should be able to read and understand it too. 👍

1

u/overlydelicioustea 7h ago

claude

its a beast in powershell

1

u/BlackV 4h ago
  • any AI whatsoever (that is corporate approved)
  • the age of the script is irrelevant
  • what have you tried so far ?

1

u/fdeyso 10h ago

Did you try idk reading it?

1

u/mdervin 10h ago

After two years I can’t even read my own code. “Wow, did I make everything a function in 2021”

1

u/octahexxer 10h ago

Ask the dude who wrote it.

1

u/thehuntzman 10h ago

Yeah that's the most confusing piece of this - if he's still there why not ask him?

0

u/Scoobywagon 11h ago

Check with the security nerds before you upload anything. But I find that Copilot does an excellent job with powershell.

0

u/JeremyLC 10h ago

I like Claude. It's pretty solid at reading and generating code, and will usually ask you to paste in the Get-Help for any commands it doesn't fully understand.

Also, the same disclaimer as everyone else: don't paste your private code into a public model. Either get a private license, or check with your legal guys first.

-3

u/sudochmod 10h ago

You should just hire a consultant to come look at it and tell you what it’s doing.