PowerShell Tutorials : Making your scripts into .exe (executables)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to this powershell tutorial in this tutorial i will be going over how to convert a ps1 or a powershell script into a exe so it's going to be an executable you can put it on a usb stick put it on any computer as long as you put all the dependencies in the package that we're going to be creating uh it should work on any computer so this was actually a request from al fariroca i hope i pronounced that correctly um but i thought this could actually benefit quite a lot of people so let's go ahead and let's get started so what i'm actually going to do we're not going to be making any powershell scripts in this video instead i'm just going to pick a old project that we've done uh so i'm actually picking the csv splitter in a gui format because i figure like most of the people would want to create exes of guis so let's just open up this script here in powershell and this is the exact same script that we made in the tutorial video there's been no modifications to it so we're going to make some slight modifications to it because right now if we take this and convert it to a dot exe it will not work properly we're actually going to get some errors and it's it's not going to be great and it's not going to be very portable either so we're going to want to fix this up and then i will show you guys how to make a dot exe so first actually i'm going to show you guys what would happen if we make this a dot exe right now just so you guys can see what happens and you guys could see the kind of errors that we would get so let's just close out of powershell here so the first step we're going to want to do is search for ie express so just i express and then you're going to want to run this as administrator do not run this just by clicking it you definitely have to run it as administrator or else you will get errors in the last step of creating the cabinet file so definitely run this as administrator so the first step we're going to want to do is create new self extraction directive file and then for the package purpose we are going to leave it at the default which is extract files and run an installation command and then the package title this could be anything you really want we're just going to call it csv split and confirmation prompt we're not going to give a prompt at all we're not going to display a license agreement now in the packaged files because our csv splitter actually had other files that belonged to it so i actually see that this will actually not work so let me just copy the file that i need here so we have our xml our xaml file that we've had and we have our script and we know that our script calls our xaml file because if i open up our script here just really quickly we do see that we do reference that xaml file so we need to package this in our package so we're going to add files and we're going to add our xaml file and we are going to add our script file here all right and then we're going to click on next and now we're going to have an install program to launch and then the install program is going to give you a drop down with nothing in it but we're actually going to be typing something in here so we are going to be typing powershell.exe then space dash execution policy all one word and then we're going to do a space we're gonna put bypass then space dash file and then you're gonna put in your powershell script name so for us right now it's just uh youtube dash csv splitter dot ps1 all right we're going to click on next now for the show window we're going to leave it as default because we want to show the window if you're making a exe of a script that just kind of runs in the background and generates a file you could do it as hidden but as soon as you want like a user interaction you're going to want to leave it as default and then the finished message we're just going to display no message at all and the package name and options so this is going to be creating your exe name so we're just going to create it in the same folder here we're just going to call it csv split and we're going to do save and then for the options we are going to want to check both of these options here because we want to hide the file extraction process from the user that we don't want them to see it and we want to store the files using the long name long file name inside the package and what this does um it just kind of makes it work nicer if you do want it to work on windows 95 for some reason it needs to be left unchecked but besides that just give it a check it's a lot easier to reference your files that way so we're just going to click on yes here like i said do not check this if you want your scripts to work on windows 95 i think you would have more issues with the script itself running on windows 95 than that option because of the net frameworks that we're using in this case but we're just going to click on next here and then the configure restart we are going to do a no restart here because we know that we don't need to restart after the script runs and then save the self extraction you could just leave it at the default location it'll save it wherever you saved your dot exe and we're going to click on next and then next again it creates the package so here we have the package and if we click on it it seems to work just fine now if i actually go ahead and just click on finish here now what i'm going to do is i'm going to take away this file i'm just going to cut it out here and then i'm going to try to run this again and there we are so we get an error because in the script we are still referencing this direct file location so if we ever brought this exe to another computer and it didn't have the folder called scripts csv splitter and then this csv splitter form it wouldn't work so it's not very portable and really there was no point in us packaging that xaml file so let's go ahead and let's just delete these files that we've created here and let's open up our powershell script here to modify it so we're going to add one line at the top here so we've added the presentation framework because we do the import xaml from visual studio but there should be another thing that we also import we should be importing the system.windows.phone framework just in case that computer doesn't know where to find that type there so let's do a add dash type assembly name system dot windows dot forms and then what we're going to want to do for our xaml file is we're actually just going to want to replace the file path was just the file name because when we are running that script it's going to look in that same directory as the script is running in so if we just put the file name it'll find it no problem in that package so that's all we really need to do another thing that i want to do because as we've as we see here when we run this we actually get an output to the console um i don't really want to show this to the user so all we need to do here is just get rid of this line 26 get dash variable var underscore star once that's done we can go ahead and save this so i'm just going to save this as csv splitter just so it's a little bit of a shorter name all right so now all of our modifications are done let's go ahead and let's run the i express again once again we're going to run it as administrator we are going to create a new self extraction directive file we are just extracting the files and running an installation command our file package is just going to be csv split or let's do csv splitter and we're going to do no prompt no license and let's add our files in here so we're going to add our csv splitter our csv splitter form we're going to click on next so here again we are going to do powershell dot exe dash execution policy now the reason why we're doing the dash execution policy bypass is we do not know the execution policy that's going to be on the computer that we're putting this script on so what we're doing is we're telling powershell to open up in the execution policy of bypass so just bypass any execution policy that that person has and open up the file csv splitter.ps1 so here is our first example of we don't need to reference a file like we don't need to say c scripts csv splitter csv splitter.ps1 all we need to do is just put in the file name because it's going to be in that directory that it's running out of so once we have that we're just going to click on next show window is going to be on default we're going to display no message let's save our exe to csv splitter and save and then again we are going to check box these two boxes here we don't want to initiate the restart and we are going to save the self extraction file and we are going to create the package it is all done let's click on finish here and let's see if this runs it does run so now let's give it a test here so i actually do have a csv file here called names so we see here it does have a hundred names in here so if we pick a file we pick our names file we hit validate we see that it does validate just like we'd supposed to we pick our folder let's pick our folder we're going to pick the same folder here so scripts csv splitter we're going to split it into two files just we can easily see if it's worked split all right so it tells us it's put it into c scripts csv splitter output dash one dot csv so let's just go ahead and let's exit out of here and here we are we have our two files we have one brew 50 here and we have 51 through 100 here so everything worked great we have a exe and now like i showed you guys last time i took away that xaml file so let's take away the xaml file one last time and the script still runs so we don't get the error where it just crashes right away so that is perfect it loaded up our form so that is how you make a dot exe from a ps1 file or a powershell script file there are a lot of other ways there are powershell modules to make them the reason why i showed you guys this way is this is just built into windows as soon as you install windows you have iexpress so it's very easy to create and package your dependencies very easily i just find it easier it's already it's already there you don't need to download anything extra you don't need to learn anything crazy about it it's not in an ide it doesn't limit you to use visual studio code or powershell ise you can use any coding environment you really want and it's quite easy to use so i hope you guys like this video so like subscribe and hit that notification bell to be notified when that next video comes out if you guys have any suggestions on videos that you guys want to see or if you guys have questions that you guys want answered and maybe if i find it super useful like this one i will make a video for you guys so lik put that down in the comments below and i will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 40,432
Rating: undefined out of 5
Keywords: powershell script, powershell script to exe, powershell scripting, powershell, csv, programming, scripting, coding, automation, iexpress tutorial, iexpress, iexpress ps1 to exe
Id: _WvIpaYcjaU
Channel Id: undefined
Length: 13min 21sec (801 seconds)
Published: Thu Nov 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.