r/PowerShell • • 2d ago

Misc Converted my PS Script to PS7

I know there are plenty of AI haters out there but I wanted to get ahead of the switch from PS 5.1 to PS 7 but it's been too long since I did any PS coding and I didn't feel the urge to get back up to speed. So, as an alternative, I fed my script that's contains code for a GUI input form into Gemini AI and within seconds it converted my script to PS7. Needless to say I was impressed that Gemini AI could convert PS scripts.

0 Upvotes

44 comments sorted by

7

u/chaosphere_mk 2d ago

Gemini sucks, but glad it ended up working out for you!

2

u/Moondoggy451 2d ago

Actually, I asked Claude to convert the exact same original script and it didn't have the same look and feel as the Gemini version. When I submitted the Gemini version to Claude this was the response:

Looks: Gemini's version looks slightly better. The window has less empty space at the bottom. The buttons are larger and centered under the form. The error text uses Segoe UI, which matches PowerShell 7's default control font, while my Microsoft Sans Serif error text looks a little off next to the other labels.

Robustness: Mine is slightly safer. Its parsing is the same on any regional setting, and it rejects odd input like 1,5 in a US locale. Gemini's parser uses the machine's culture and also accepts signs (it then rejects negatives in a separate check). On a US system the results are identical. Keeping the payment at the top of the file also makes it easier to change later.

Practical difference: for your use there's none in function. Both calculate the same principal and interest.

So, what I did was ask Claude to combine the best aspects of the Gemini's version and the Claude version to combined version that has the looks that Gemini produced but with the robustness of the Claud version. So now I have the best of both.

2

u/ankokudaishogun 1d ago

..have you tried to actually read the code?

0

u/Moondoggy451 1d ago

That was the point of using AI to ensure that the script would work with PS 7. I wrote this script years ago and had a lot of assistance from some more talented people regarding the gui screen and I really didn't feel like getting back up to speed just for one script so no, I didn't look at the code.

1

u/Solonotix 2d ago

I haven't tried it personally, but all the benchmarks seem to indicate that the latest Gemini models are actually on-par with the other frontier labs and models. It's surprising after such a long drought and being stuck with the lackluster but sufficient Gemini Flash 3.1 (or whatever it was) to suddenly get 2-3 models from Google that are all respectable.

1

u/Moondoggy451 2d ago

See my reply to chaosphere_mk

1

u/YT-Deliveries 2d ago

Jury is still out, really. From what I’ve been able to gather, Claude Opus (via Claude Code in VSC) seems to still be the go-to for most people.

Full disclosure though that I spend a significant portion of my day doing that exact thing.

1

u/Moondoggy451 2d ago

See my reply to chaosphere_mk

2

u/Solonotix 2d ago

I have to toe the line with how I phrase this, but I wish I could do that at work. What I mean is that I don't really like AI in general since I can usually write better code than it (eventually), so I still prefer to write it at least in part. But also, I'm not allowed to use anything at work except Amazon Kiro. That would be fine if we weren't on some old subscription that locks us to nothing newer than Claude Opus/Sonnet 4.6 and Claude Haiku 4.5

On the same note, this would also be fine if we weren't expected to use Kiro in our daily work. So I'm stuck using a sub-par harness, with sub-par models to try and be productive. And that's the context for why I say that "I wish I could use Claude Code (at work)" 🫠

2

u/YT-Deliveries 2d ago

I always say that Claude is like a very smart intern. It's really good at doing research, coming up with methods, and writing the code. But you still have to keep it within good process workflows and "keep an eye on it".

Once you've got a good workflow of QA checks, "dos and don'ts" and project parameters though, the amount of time it saves is hard to overestimate.

As to your point, though, you could do worse than Opus 4.x

Since you're using Kiro, maybe they'll let you play with Bezos' new toy they unveiled yesterday :D

2

u/BlackV 2d ago

Yeah, that's fair, sometimes you have to use the tools given

2

u/DoctroSix 2d ago

Try out AntiGravity, it's google's version of Claude Code (which is also damn good).
It can spit out a script to a folder for you, while chaos erupts elsewhere.
Then you can calmly look it over in vs code while sipping coffee, making sure it works as intended.

I've been coding in Powershell for over 7 years, and it's amazing how those AI tools help you quickly turn ideas into a robust script.

1

u/Dapper_Librarian388 2d ago

I dont think Gemini sucks in Powershell. Something like Powershell coding is already an easy task for all top AI like chatgpt, claude, gemini.

1

u/Moondoggy451 2d ago

See my reply to chaosphere_mk

0

u/Personal-Cup4772 2d ago

This guy never used Gemini models

1

u/Moondoggy451 2d ago

See my reply to chaosphere_mk

17

u/Modify- 2d ago edited 2d ago

Suggestion: only update the script(s) if you use commands that are deprecated. I think most of it will still work without changing anything.

0

u/jasonvelocity 2d ago

*deprecatedĀ 

13

u/BlackV 2d ago

What actually changed though?

What specifically was ps 5 only and what specifically is ps7 only now?

14

u/johnnydotexe 2d ago

They can't answer that because AI did it for them.

1

u/BlackV 2d ago

Quite possibly, I guess at the very best they're moving the process in the right direction if they're switching to 7

6

u/ankokudaishogun 2d ago

What actually changed though?

Plot twist: AI did change nothing because the script was already Core-compatible, but OP never noticed because he didn't review the code

1

u/BlackV 2d ago edited 2d ago

I was thinking maybe it changed winforms to xmal/wpf?

But yeah in a script unless you're using parallel or the newer ternary operators there would likely be nothing 5 or 7 specific

1

u/-Invalid_Selection- 2d ago

A lot of network calls for us had issues so we had to update for that. There's also the looming end of wmi for cim, with cim being more featured in 7. Most stuff works the same though.

We did have to also normalize the outputs due to changes on how some things reported results.

3

u/BlackV 2d ago

There's also the looming end of wmi for cim

Looming? Since PowerShell 3 is when that change should have been made

A lot of network calls for us had issues so we had to update for that.

Interesting, What do you mean network calls? That around cimmseaaion or similar?

1

u/-Invalid_Selection- 2d ago

We pull things directly from websites and the way 5 handles that vs 7 is different. It's easy to fix for, but we have powershell code going back to posh's initial release and we've been pretty good about keeping things updated while maintaining compatability.

We did finally put 3.0 on the chopping block thankfully if maintaining compatability meant being unable to reach the goal of the script, but we support too much legacy stuff to even fully get rid of that. And the looming is because unlike with posh 5 to 7 change, they told us back then with wmi and then kept shipping it instead of cutting it off.

1

u/BlackV 2d ago

Ya and the recent (relatively) changes with -usebasic on invoke-webrequest

1

u/KevMar Community Blogger 14h ago

The one area I test the closest is code that uses Invoke-WebRequest without the -UseBasicParsing or anything related to error handling bad requests. The internals of that and Invoke-RestMethod went through a lot of changes and the different dotnet objects are shaped slightly differently.

-1

u/chrusic 2d ago

For the powershell language itself, not that much.Ā 

However, ps7 runs on .NET core, while ps5 uses .NET Framework, and not all functions were included in the rewrite from Framwork to Core. Core is OS/platform agnostic, runs on Mac/linux etc.

In day to day operations you'll rarely hit any snags, but there might be a missing module here or there, and some WMI functionality on Windows is no longer supported.

Pwsh even runs ps5.x modules loaded in a session called WinPSCompatSession when it facilitates legacy modules for you. You can also use it yourself with import-module -name "somemodule" -UseWindowsPoweshell.

The real issue is trying to run pwsh7 scripts in ps5, that can easily break due to new functionality that ps5 doesn't have and won't get, like ?? And variables like $iswindows/$ismacos etc.Ā 

1

u/BlackV 2d ago

Ya that's why I wanted specifics

6

u/mitharas 2d ago

Powershell is rather good in backwards compatibility. So I wonder what needed to get changed? Did Gemini just give you back the original script?

1

u/Moondoggy451 2d ago

Interesting event. I had another script that I converted to PS7 last night that read a huge nightly log file of my MPS360 backup to the cloud and it successfully converted it just like it did my other script but like my other script I fed the original to Claude and what Claude spit out wouldn't work. I then fed Claude the Gemini converted script and Claude created another version that didn't work. Claude said that it could make the improvements to the Gemini script and it spit out yet another version of the Gemini script but whatever it added or changed caused the modified Gemini script to fail. When I say "fail" I mean did nothing. When I told Claude the improved Gemini script didn't work it just gave up and said "Use the Gemini version since it work".

1

u/BlackV 2d ago

without seeing the code, seems odd

1

u/KevMar Community Blogger 14h ago

Add the Microsoft Learn MCP to your session and tell it to use that for PowerShell reference and example code. That makes a huge difference.

1

u/itscum 2d ago

If you want your ai to be good at PowerShell then just give it reference material. I at least drop in the IT Pros automation book and give it the direction to reference the book when dealing with PowerShell. This works on all the models I use

1

u/ArieHein 2d ago

For that task, using ai to convert isn't an issue. It might not be the most optimal code, but thats not something you care about if it works.

Where its nice for you to test is from a new session that doesn't have memory of your code, describe your application or ui as best you can and let it create it from scratch. Now compare between results.

When the actions the code choose are potentially harmful or irreversible, look at the cmdlet that the ai used, specifically in the documentation that it didn't add or forgot to use specific flags or parameters.

You can even ask your ai if it can also elaborate why it choose specific cmdlets and the specific parameters. Use that as a learning tool.

0

u/Moondoggy451 2d ago

Note that my goal out of this was to ensure that my script would run in PS7 as I rely on it monthly and didn't want Microsoft to drop PS 5.1 and end up with a surprise. I did go back and ask Gemini what problems it uncovered in the original code and the response was:

Nothing in your original script would have stopped it from opening or running in PowerShell 7.

However, your original script had a few "hidden" bugs under the hood that PowerShell 5.1 let slide, whereas PowerShell 7 is much stricter. Here is what the new script actually fixed:

  • Doing math on text instead of real numbers: Your original script turned the dollar amounts into formatted text (with commas) before doing the subtraction. If your principal payment ever hit $1,000 or more, the comma would have broken the math. The new script does the math with real numbers first, and adds the formatting at the very end.
  • A typo in a true/false check: Your original code used a single = instead of -eq to check if the numbers were valid. In PowerShell, that accidentally forced the check to always say "true" even when the data wasn't in the right format.
  • "Smart" quotes: The word 'Calc' in your original file used fancy, curved quotation marks (like from Word) instead of plain straight ones, which can trip up PowerShell 7 depending on how the file is saved.
  • Leftover "dead" code: There was a command at the very bottom telling the script to pause for 160 seconds if an "OK" button was clicked—since your form doesn't have an OK button, it never ran, so we removed the clutter.

In short: your original script would have launched in PowerShell 7, it just had a few ticking time bombs in the math and formatting that are now safely defused.

1

u/BlackV 2d ago

Good as gold, do you use Gemini just cause or is a work tool? (Same I'm allowed ms copilot)

1

u/Moondoggy451 2d ago

I use Gemini and Copilot back and forth but I tend to use Gemini on stuff I feel is a bit harder.

3

u/Flettys 1d ago

Cool, it ignored your request and made unrelated (but helpful) changes.

-1

u/keith-kld 2d ago

As I know, Copilot is very good at powershell script. Leo (Brave browser) is very bad at script. I have not tried chatGPT, Gemini, or Claude. even though they give you the script that can be run immediately, you should find out the gaps and errors, restrictions, etc. which can be derived from the script. Then improve it of your own accord. Note: The script given by AI is normally not optimized, or even bad or redundant. Human will check and improve it.

2

u/johnnydotexe 2d ago

Copilot has sucked for pretty much anything Microsoft any time I've tried it over the last year. Claude is far better, and you can even add the MS Learn MCP to it and system-prompt it to check there first for MS questions before scouring the internet.

0

u/keith-kld 18h ago

I have just tried Google Gemini, it is also good at powershell script, or may be even better than Copilot. The response is clear and straight forward.

1

u/BlackV 2d ago edited 2d ago

It is not good at powershell at all

Usable, yes, good no

Would be nice if it could be pointed directly at a MCP server

Edit: wait.... Microsoft 365 copilot or GitHub copilot, apologies I realize now that I just assumed it was 365 copilot