Automate your PowerShell scripts with Windows Task Scheduler

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's show we wanna how save our powershell script to a file and then we're gonna run that file using a Windows scheduled task so that way you can get an automated solution right you want to play hooky but still get your powershell scripts to run this is the video for you but first here's our intro [Applause] [Music] hi my name is Shane Young with bold zebras those guys in today's show is all about taking your powershell scripts and saving them to a file so then that way we can run them by a schedule task so we'll walk through saving a file and then we're going to take and make that a scheduled task we'll make it run every 5 minutes just so we can see it run but it should give you all the mechanics that you need so you can make more automated solutions right back when I was a full-time administrator where I was all about how do I automate solutions so it does the work for me so I could you know work on my golf game played more solitaire look at funny cat videos on their net whatever it is you want to do I want to help you do it more with PowerShell and this video comes to us thanks to Kenny I don't want to give Kenny's last name and where he's from because I haven't heard back from if he wants to plug but either way mystery Kenny if you're out there as videos for you alright this is a reminder that do you guys have questions leave me comments send me emails tweets that type of stuff Kenny asked just a great question I was like man that'd be a good topic for video so I was like give me a day I'll pop the sales video for you real quick not actually for him I might undo his job but I'm gonna show him all the foundational stuff I'm gonna show you guys at the same time so leave me the stuff below and we can always hook up where it makes sense all right that's enough jibber-jabber let's just dive in by switching over here to my desktop and so on my desktop I've just opened up the PowerShell ISE and I've just got a couple of quick lines that I wrote nothing complicated here right you could do any script you want but I just want to do something that would produce some output so we could take a look at it what did I do here so I said git date and then write that out to a file named e colon demo we'll call this log for video right we'll rename it log for video text and - append so this going to get the current system date and then output it to a file called log for video in the second line get PS Drive says kids all the drives all the file system drives on a machine and then it takes those and it only gives us the ones that are the file system provided right let's run this real quick same seat it looks like so highlight just that hit run and so you can see it's like right your C Drive you got a hundred six gigs free your D Drive you've got almost nothing free e F so it just runs the different system files now I could then take that and I kind of mess with so earlier I ran out of time but you could pipe this over to select and only select the particular properties you want or reality if you're gonna make this I try log it to an Excel workbook right via a CSV and we've covered all of those topics before in other videos so check this out if you're like yeah that's what I want to do don't worry I've covered how to write to CSV file and I you know format that you can control and uh translate from this so anyway we're gonna take that output we're gonna write that to log for video also okay so append that so let's run this whole thing one time and see what it should happen so say run our script and we'll go over here and so there's a log for video just create us a file if we double click on that I'm gonna see that it walks Saturday February 10 that's right dedication here I'm making this video on a Saturday cuz well dinners not ready and I had a few minutes so I thought I'd make you video but anyway and then you can see then it followed up here and so if we run it a second time when it would close this run the script again boom it ran again we'll double click on it and so then now you can see there's another entry right this was at 6:09 p.m. in 25 seconds it took me 24 seconds to get around running in a second time and then there's all the same information anyway we don't really care right this video is not about that simple little script what this video is about is now that we've figured out the power shell that we want to run on a daily weekly hourly by minute basis how are we going to take and automate that so the first step is we're going to take all this and we're gonna say save and I've already saved this almost of a save as this time but I must say this the ecole and demo and I'm gonna call this video PS right video powershell terrible name but it works cuz i'll know what it was you probably wouldn't say save okay so that was step one we saved this PowerShell into a ps1 file so if we look over here there is a file video PS and dot ps1 if I didn't type it and I should've and so that type of file power show can run writes PowerShell script file so let's minimize this let's minimize this and now let's type in start aimless do with spell tasks correctly let's run the task scheduler now I will make this a little bit bigger so I am NOT a task schedule guru so I'm not going to try and tell you the intricacies of all the cool things you can do here there's a lot you can do here what I'm more of the style of is let's create a basic task my basic kind of guy so we're going to create a basic task click a little link over here we're gonna give it a name and call it video automation we could give it a description Shane made this you'd probably want to make your description a little more descriptive we'll say next what's our trigger how often do I want to run this I want to do it daily so we'll say next and then do you want recur every day you know you can kind of get into really make these schedules anything you want but we're just gonna go the simple recurs every day and we will do it in a six twelve thirty eight alright so one minute from now hopefully I get this done in one minute so we'll say next what actions I want I want to start a program so hit next so for program here what you need to type in is PowerShell right and so PowerShell dot exe is in the system path so it's just automatically gonna run PowerShell dot exe and then what we're going to do is we're going to pass the PowerShell dot exe de : demo what did I even name this file eco land demo video PS PS one video PS PS one so that says pirate PowerShell with that parameter which will run that script let me say next it's like hey are you happy with all this I am so we'll say finish now it took me a minute earlier to figure out where they were hiding these right back to I'm not a PowerShell task edge or MVP but if we double click on this guy we then click on the task scheduler library in here you'll see my video automation script right so we run at 6:12 Oh looks like it next run time six twelve thirty eight alright so it's coming gotta go faster but if yours isn't showing up I had this problem earlier today right quick right here and then do a refresh and then you should absolutely see what you created right well protip I do know some things about task scheduler so now if we minimize this and we wait just a few seconds let's see six twelve oh it looks like it might array let's double click on it so there's our six nine six nine six twelve thirty eight boom it's logged right so now tomorrow at six twelve thirty eight it's gonna run and put that in there and that's really all you need to know right now we've got a PowerShell script that is running on a scheduled task that we've set up you can get into scheduled tasks right if we go back over here to task scheduler and you can right click on this guy and go to properties and so then you can see there's a whole bunch of stuff you can do here right so if you need to do more complex things but I'm not the right guy teaches and I can bother but I've gotten you to a PowerShell script that will execute on a scheduled tasks I feel like my work is done I'm starting to smell dinner so hopefully this was the answer that Kenny was looking for and hopefully the answer you were looking for so Cammy other ideas down below you never know when your idea might turn into these videos so thanks and have a great day hey it's me again just a reminder if you don't mind click the old subscribe button over here that always helps me out or if you want to work together you can always hit me up through the bold zebras or if really what you want is some more of these power app videos which is probably what you want then the playlist is somewhere on the screen here alright thanks have a great day
Info
Channel: Shane Young
Views: 83,853
Rating: 4.8642859 out of 5
Keywords: powershell scheduled task, powershell scheduled job, powershell scheduler, shane young powershell, shane young, bold zebras powershell, task scheduler powershell, task scheduler windows 10, powershell ps1, save powershell, get-date, get-psdrive, psprovider, out-file, out-file append, run a powershell script, run a powershell script from a batch file, run a powershell script task scheduler, powershell scripting for beginners, powershell tutorial
Id: GOkAy2RqRQk
Channel Id: undefined
Length: 8min 24sec (504 seconds)
Published: Sat Feb 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.