PowerShell Tutorials : Making a GUI Part 1 - Introduction & Hello World

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this new powershell tutorial series in this powershell tutorial series we're going to be going over how to make a gui in powershell uh so graphical user interface now a lot of times powershell is mostly regarded towards scripts and automation but if ever you have to make a script that you're going to be handing to someone that might not really know powershell or doesn't feel quite comfortable yet in powershell or even if you are using the script yourself or giving it to people that are super familiar with powershell making a graphical user interface could easily accommodate those users that aren't really comfortable in editing scripts if they have to edit uh values to make slight changes and just have a script run ad hoc or if you're making menus in your script and you're making it like a dos based menu graphical user interfaces could just make that a whole lot easier to manage um and just go through and just makes the user experience so much better now there's a few ways to create a graphical user interface with powershell we're going to be going through quite a few different ways in this whole series at first i'm going to keep it really really simple we're only going to be using powershell ise and we're going to be building the forms strictly through text and through code and then eventually we will be actually using visual studio code wpf so windows presentation forms i believe that's what it stands for we're going to be using that to create a xaml file and we're going to be using the xaml file to generate our graphical user interface we're going to see why we want to use that eventually um to make graphical user interfaces in powershell it is somewhat easy once you get a hang of it but if you're making more complex guise it could definitely get quite lengthy in coding it by hand in powershell so in this first video i'm just going to be going over some basics how to make a window pop up and we're just going to make it show hello world the very very standard first project to do now i'm not going to be going over any of these like normal powershell commands in this video uh so i will be linking down below in the description the links to my powershell tutorial series for beginners and advanced you really only need the beginner one for this one i think but definitely the advanced one the intermediate one will not hurt you'll be able to make more complex forms you'll be able to understand a little bit more with the different functions so the intermediate one might be kind of handy because we're going to be getting into some pretty deep uh powershell pretty soon but i just want to show you guys some examples of what we can make with some guise i have some very simple examples to show you guys first and then we're going to get started with our first one here so this one is a very simple one that we will be building together it won't be our last project i think this one i have it planned for the third or fourth video so let's actually just zoom in here to see what it does so i called it a service inspector so of course this isn't like a super handy script but it was really to show you guys uh the different drop downs that we can do so basically what this does it loads this drop down with all the services on my computer and then if you select one so we can actually type in this drop-down and we'll get to that we can pick the print spooler and then it will actually give us the like the display name which is print spooler instead of the service name which is spooler and it gives us the status of running and if i pick a service that's not running like bits it's actually in red so we can do quite a lot of cool things with the gui's of course this is a very very simple example i'm thinking about making i haven't made this video yet but i'm thinking about making an example maybe uh combining some active directory in here and maybe doing like a locked out user interface so we can see the locked out user and maybe some information about that user and then an option to unlock that user right from the window maybe change its password or disable the account to protect it so that's basically it uh today we're going to be making uh something that looks like this so just a very simple hello world graphical user interface and i'm going to be showing you guys how to bring that up here so first thing we're going to want to do is we're going to want to open up a powershell window so i do have that here just a powershell ise will do and you don't need any prerequisites installed so as long as you have powershell windows 10 you're pretty much good to go you don't really need windows 10 i believe that you you could run this on any kind of like server os um so that's pretty much it so let's get started so the first thing we're going to need to do is we will need to add a type in here kind of similarly to what we did for the selenium but this type is actually already built in to the system so we're going to do add type and we're going to give it an assembly name and we're going to give the assembly name of system dot windows dot forms so this basically tells it to bring in uh the windows forms type so we can build out those forms that we saw so one of the things that i like to do now this isn't necessary but i really prefer to do this because it saves you a lot of typing later on is i actually like to create objects of the different type of components that i'll need in the project so what i mean by that is we're going to create a form object and we're going to set that equal to open and close square brackets we're going to put in here system dot windows dot forms dot form and then for this project which is just going to be the hello world object we are going to create a label object and we are going to give this one system dot windows dot forms dot label so basically what this is this brings in our forms this brings in our label object and a label is basically just something where we can put text in it's not a text box it is just simply a label so an example of a label would be this hello world we can see the text we can't really do anything to it all right so what we need to first do is we need to first create our form and then we need to place the label in the form and then give some text to that label so we're going to create our form here and we're going to create our form which just hello world form you could give any name you really want this name is kind of long so probably not that but we're going to do a new object and we're going to call the form object so basically what this form object really does it just saves you from having to rewrite this all the time now for the form object it's probably not super useful because we're not going to be creating a whole bunch of forms unless you're creating multiple different windows for your project then it would be useful but every time you're creating a label you really don't want to type this whole square bracket out all we're going to have to do is reference the label object so that gets kind of handy there all right so then what we're going to want to do is we're going to want to define some of our properties for the hello world form now what i like to do is if we actually go here hello world form and we do a dot we can't see anything of what's available to us and that's because the type isn't added and we don't know what type of object this is so what i like to do is i like to run this it loads it up and we have tons of options here so we're going to look at client size and what we're going to do is we're going to set this equal to a string and then the it's going to take in coordinates so just your simple x y coordinates um so what we're going to do is we're going to do 500 by 300 and this will give us a very very similar window to this so we have oops i kind of resized it there a little bit hopefully it's back to normal so we have our first value which is the x value which is the length and then we have the height value or the y value which is 300. all right and then what we want to do is we want to be able to change the text that's at the top of the window there so if we do hello world oops hello world form dot um and i believe it's text here and we're gonna do this to a string hello world we're just going to give it a hello world tutorial here and then we are going to give it a simple back color of white so that's the background so back color and then we are going to set that to the hex value of white which is just six f's here perfect all right so now that we have our form now what we need to do is we need to display it because if we run this code you will see that nothing really happens so what we're going to want to do is we're going to want to do a hello world form dot show dialog and then open and close brackets and then what we're going to want to do is we're going to want to make sure that we after the show dialog we're going to want to make sure that we do a dot dispose open and close brackets on that hello world form this is just to make sure that it cleans it up so this is just cleans up the form and then this will display the form all right so now if we run this here we actually do see our form so that is perfect now you don't need to use this hex code if you did red here it would change it to red if you wanted to simply type in white it does white of course you're a little bit more limited to the colors here because it might not have all the colors that exist in a simple word um so if you want to specify very exact colors i would recommend hex you can easily find hex values of colors online by simply googling like hex colors and then they'll usually have like sliders that you can use to really kind of pinpoint to your exact color that you want and you'll get the hex value for that color so now what we want to do is because we get our form but we don't have our label on there so what we want to do is we want to create a label object so what i like to do is i like to give all my labels a variable name of labels and we're going to name this label title and we're going to create a new object label object and then label title dot so here again when we have the dot we don't see anything once again what i like to do is i just like to run all of this and now we have all the options so let's do a text and let's put this to hello world and then we are going to do a label title and then what we're going to do is we're going to do an auto size and we're going to set that equal to true now another thing that you would be able to do instead of auto size is label title dot width and dot height now the only thing about that is that you'll have to play around with that a lot to get it to the right size in order for it to show all the text properly i prefer to just use auto size it auto sizes the label to fit the word it's a lot easier when it comes to placing especially when you're using powershell to lay things out so now what we're going to want to do is we are going to want to do a location now the location is a little bit different we're going to want to do new object we're going to do system dot drawing dot point and we're going to put this at 20 and 20. so this is the 20x and 20y so it should probably be at like the top top left-hand corner of the window and now if we run this here we will notice that we actually do not see our hello world now this is because we created the label object we gave it the location but we actually never placed it on our form so in order to add it to our form here we can do hello world dot form and then dot controls and then we can do a dot add now you're going to see there's an add and add range now in add range we can see takes a array of controls and the ad just takes one control i would always do add range or if you want to add it every time that you create an object by all means you could use add i always tend to use add range and then put a at symbol there open and close parentheses and then close my parentheses and then in here i just put in my label title and then we hit start and there we have it so now we do see that this hello world is a lot smaller than the one that i had in the example um if i run the example here we can see that they are vastly different um so one is very big one is very small so the way that we can actually change that is if we come here we do label title dot font and this is going to be equal to a string again now the way that this string is made is actually font size style equals style so what this means is that your first item is going to be the font so let's do verdurna and then the size of the font so let's just start off with 24 and then the style is we can put bold you can put italic or anything like that in there as well so now if we run this all right so something i did here is wrong and i believe it's because style is actually supposed to be lowercase yes so it is case sensitive so style does need to be lowercase and then we get our hello world in bold at the top left now what if we wanted it centered this is where the drawback to the powershell system way of building out a form is because we're going to have to kind of figure out a location here so let's put this to a hundred see where that puts it all right so it's a little bit closer to the middle for the x value so let's do 120 that seems pretty centered and then let's do this to 90. that looks a little bit more maybe let's do 110. that looks pretty centered to me so 120 and 110 and that looks pretty good it might not be dead center um but it's definitely good enough for me for this one here so as you can see when you do get like a lot of controls it might get a little bit hard to start placing stuff but it's still very very easy the first few tutorials we will be strictly using powershell ise because i believe that doing it this way you really get to learn how making gui's really works in powershell now this was a very very simple example in the next video i'm going to be showing you guys how to do hello world again but we're going to be adding a button so at first the the window will be blank you press a button and that will make hello world appear and then what we can even do to add a little bit of complexity if you press the button again it'll make it disappear and then reappear and then disappear so we can definitely do that and that will show you guys how we can add functions to buttons how we add buttons to it but this is pretty much how you build a gui in powershell so i hope you guys enjoyed this video um there will be tons more to this series because i find that when i try to learn how to make guise in powershell there was a few tutorials out there but they didn't really cover the basics they kind of jumped right into very nitty gritty um making forms with posh gui and making forms with visual studio um and they jumped right into making like full-fledged systems and i just find that really confusing myself so i really want to break it down for you guys so in the first video as we saw we just made a simple form in the next video we're going to be adding a button we're going to be adding some functionality to it in the third video we're going to be adding in some other types of controls we'll probably see the drop down which is actually called the combo box in that video and we're going to see how to dynamically load that combo box and maybe do a whole bunch of fun things and then i'm going to be showing you guys how to make forms with visual studio visual studio and then exporting that code and bringing it into your powershell code and then creating the gui based on that and then maybe we can do a project based gui learning with making something for active directory right now i'm in the middle of moving right now um so you're going to see them it's probably going to be like two videos a week for now um but don't worry like i'm still making these videos uh so definitely like hit that subscribe button hit that notification bell to be notified when that next video comes out and i will see you on the next video
Info
Channel: JackedProgrammer
Views: 43,840
Rating: undefined out of 5
Keywords: windows powershell, powershell basics, powershell scripting, powershell gui, coding, programming, scripting
Id: X6nU5QCF8XI
Channel Id: undefined
Length: 21min 26sec (1286 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.