Jump to content

Powershell GUI script help

CloseKoR
Go to solution Solved by Franck,

Well i would honestly think about using something else than PowerShell. Here you are running Batch file and not PowerShell file but anyhow i would use a language geared toward UI.

 

You could in maybe 15 minutes (for a newbie) do that in simple language like C#.

1 - You just pick the winform template project and it will create a single window and open it in the editor.

2 - drag drop a button control from the toolbox (on the left i believe) onto the window and size it with your mouse

3 - click once on it to edit the text value that is in the "text" property in the property grid on the right'

4 - Double click the button to open the code to perform when you click it

5 - run the batch you want with 1 line of code. Most likely Process.Start("C:\\Users\\Users\\Desktop\\Restart_Spooler.bat");

6 - create more button and trigger more script.

7 - press F5 to build and run the code

8 - open the project folder and in there you have an YourProjectName.exe ready to use

 

If you want to go deeper in the futur and actually run PowerShell commands, in C# you can run these command directly using the same way of writing but using the API functions.

Good Morning! 

 

I'm hoping im posting this in the right part of the forum, if not, let me know and i can make a new post in the right spot. 

 

For starters, super new to using powershell scripting (and any programming language for that matter), and this has kinda turned into my pet project to keep learning. 

 

So, essentially, I wanted to make a GUI that could be ran by anyone in my workplace to execute simple commands for now. I would like to expand it to other things in the future, but for now, just things like dism/sfc, gpupdate, etc. Also, yeah, its pretty useless for anyone who just memorizes the commands, but I work for a school division that really under-appreciates their IT staff, and our entry level position starts at 19k a year, full time, for work that is way above that pay grade, so we get very inexperienced/ uninitiated hires (understandably so). I've been the one taking the initiative to try and make their lives at least a little easier with better documentation, resources, communication and hopefully, this script. 

 

What i'm trying to accomplish is packaging the batch files i've made with my script, either in an exe, xml, whatever it needs to be, so that way when I send it to someone, they don't need to edit the script to put in the address of the bat file, which defeats the whole purpose of the file. I've tried looking elsewhere, seeing suggestions for dot-sourcing, then using powershell pro tools to make a exe that attempts to gather the dot-sourced files, I've tried using ps2exe hoping that it would just magically want to grab them for some reason, i've attempted to skip the bat file and use cmd.exe /c to just execute the command, but i'm starting to think i just don't understand how that is supposed to work, since the examples i've seen of that in action had the batch file address after it anyways.

 

Also, this is the part that I think is useful for this problem, i'm not sure what exactly is needed so i'm just trying to give as much info as I can provide.

<

$btnsfc.add_click({start-process C:\Users\Users\Desktop\DISM-SFC-SFC.bat})
$btngpu.add_click({start-process C:\Users\Users\Desktop\GPUPDATE.bat})
$btnchk.add_click({start-process C:\Users\Users\Desktop\_CHK_DSK_.bat})
$btnprntspl.add_click({start-process C:\Users\Users\Desktop\Restart_Spooler.bat})
 
I also attached the full script in case it was needed, I'm not sure how kosher that is so if it isnt im sorry in advance! 

Is this something that just isn't doable because of powershell? Or am I just approaching it wrong?  

 

PS GUI(1).ps1

Link to comment
Share on other sites

Link to post
Share on other sites

Well i would honestly think about using something else than PowerShell. Here you are running Batch file and not PowerShell file but anyhow i would use a language geared toward UI.

 

You could in maybe 15 minutes (for a newbie) do that in simple language like C#.

1 - You just pick the winform template project and it will create a single window and open it in the editor.

2 - drag drop a button control from the toolbox (on the left i believe) onto the window and size it with your mouse

3 - click once on it to edit the text value that is in the "text" property in the property grid on the right'

4 - Double click the button to open the code to perform when you click it

5 - run the batch you want with 1 line of code. Most likely Process.Start("C:\\Users\\Users\\Desktop\\Restart_Spooler.bat");

6 - create more button and trigger more script.

7 - press F5 to build and run the code

8 - open the project folder and in there you have an YourProjectName.exe ready to use

 

If you want to go deeper in the futur and actually run PowerShell commands, in C# you can run these command directly using the same way of writing but using the API functions.

Link to comment
Share on other sites

Link to post
Share on other sites

PowerShell is generally intended for shell scripts, meaning stuff that runs in the terminal, not for building UI applications. I would second the recommendation to build a simple WinForms app in C#.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you guys for the suggestion! Truthfully had no idea, was just learning powershell and trying to dig into it because that’s what I was recommended for moving up from my current position, like I said, newbie. 
 

I’ll start diving into C# then!

 

thank you so much! 

Link to comment
Share on other sites

Link to post
Share on other sites

Just to offer a different perspective, as someone who has done something similar using CMD and PowerShell, you may want to consider whether a GUI is really necessary.

 

Don't get me wrong, if you're doing this to tinker and you're finding that you enjoy programming, have at it! C# is great and Visual Studio makes it trivial to create a simple WinForms app.

 

That said, PowerShell is very capable and the types of tasks you seem to be looking to accomplish can be handled easily via the command line. That will probably be easier to maintain and - the biggest benefit of shell scripting - trivial to automate or even run remotely. If you want user interaction, for instance having them select which specific script or task to run, you can just read user input. Something like a simple list of tasks with numbers and a prompt to type the desired number can be done pretty easily.

 

PowerShell also has a concept of modules (.psm1 files) that can be loaded onto a user session scope and called from other scripts.

 

The biggest hurdle I'd wager you could run into vs. something like a WinForms app is that Windows locks down PowerShell script execution by default. There are definitely ways around that, though.

 

One other option, albeit one I don't know much about personally, is Power Apps. You may be able to get what you're looking for without having to do much (or any) coding using a low-code platform like that which might make maintaining and updating these tools more accessible to the rest of your team.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×