Powershell Sripts with Task Scheduler
In today's show, we wan na how save our powershell script to a file and then we're gon na 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 gon na show him all the foundational stuff I'm gon na 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 gon na 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 gon na take that output. We'Re gon na 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 gon na 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 gon na 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 gon na 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 gon na, 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 gon na 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 gon na 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 got ta 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 gon na 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.