r/PowerShell • u/Moondoggy451 • 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
7
u/chaosphere_mk 2d ago
Gemini sucks, but glad it ended up working out for you!
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.
13
u/BlackV 2d ago
What actually changed though?
What specifically was ps 5 only and what specifically is ps7 only now?
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/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:
=instead of-eqto 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.'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.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/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.