Work with Date & Time with PowerShell

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and today's show we're going to learn to work with a date and time object in PowerShell things like adding to or subtracting from a date or time for mounting the output so it looks nice and pretty the PowerShell methods and properties you can use and some of the ways you might use these newfound skills with event logs or editing files face it date time is a daily thing so learning more about how they work in PowerShell is always helpful but first our intro [Applause] [Music] after that music who couldn't be ready to learn hi my name is Shane Young with bold zebras those guys and so we're gonna do today is we're gonna jump in and talk about date and time right now because it's the fun exciting thing you usually have projects that you actually care about at date and time but when we work with power shows a lot of date and time stuff we need to do right sorting log files pulling files from the file system to do different things with them copying things so this is kind of laying some those foundational knowledge so you understand how date and time works so as you do your real projects you can kind of plug and play these pieces should be a pretty interesting little topic should be pretty quick too so that's the good news to get started let's launch powershell someone start i'm going to type in power show and we will double click on the icon there or single click whatever one works for you so here greeted with our PowerShell prompt and the first thing i always want you to do if you've watched any of my videos right is to type in start transcript so start transcripts it's going to give you that running long very things you're done here so as you learn new things you haven't documented or if people accuse you of messing everything up by all your playing you have documentation didn't mess it up all so then the first thing i think we're trying here is just a little get date so typing get date will hit tab complete perimeter so there you go we can see it's like hey it's Wednesday April 12 2007 teen at 11 1951 a.m. goodness a lot of information there but it works for us and so the first thing I like to do here is I'm going to put that into a variable so I'm going to say dollar sign Pig equals get date right old tap complete again and now if we type in dollar sign Pig we have that same date time jekt and the reason for that is I wanted it's a little easier to work with especially the properties and things like that when we have it in a variable you don't have to do that when you're learning but it makes it easier so the first thing we'll do speaking of the properties is when do a dollar sign Pig and we're going to pipe that over to select star and hit enter and so there's all of the properties that have values so the great thing about this is this gives you kind of that first set of information you can start learning and looking you know so for example maybe you're really just interested with the day of the week is because you need to write down to your local or your powershell script there it is it's Wednesday it's a property or what month is it what day is it that type of stuff so a lot of different things there and the easiest way to use those now as you just do Pig they have week right little tab complete and so then there you can see that that particular date is Wednesday pretty easy to use you also might try something like Pig and then month and so here you can see that month is currently represented by a four so here in a minute we'll look at how do we get that so it says that it's April because April or maybe the short brigades APR might be a little more convenient for you all right so let's clear our screen so we jump back up to the top so the next thing I'd like to do here is we're going to go with Pig and get - member Oh lots of information so scroll back up so if you're not familiar with some of these things I'm doing like the piping or the properties right there are the beginner powershell videos if you check out the YouTube channel that have the kind of core basics but I'm assuming you've watched those see how another standing start need to explain what pipe does but we didn't get member here because we want to look at the things the methods right and so the methods are the different ways that we can work with it's right the actions that we can take on the system date/time object that we've currently have stored in the variable Pig so we can add days add months add hours we can do things like - long date in two short dates or it's a short time and date right remember date is both time and date so those are the methods and then down here it shows the properties which we already talked about right when we did these select star which showed us all of those so one of the things that you might want to do here is you might want to say something like dollar-sign Pig too short date just like that and so then there is the short date version of the pig vehicle so everything's still in the variable right but that's just returned the short version or we could of course change the word short to long and so then there's the fully articulated version of the date the same type of thing is available for time right so we can do change or date time and so then there's the long time so you know nice easy ways to pull the things out if you're looking to have those converted to that very specific format the other methods that we might look at are the adding alright so maybe need to add time does so if we say add there's a little tab there add months and for what are we going to get well now instead of April it's August right because that's four months later but if you do dollar sign Pig it's still sat April right we didn't change our variable we just said take what's in that variable and add four months to it for the output so for example if you're trying to create a script that was you know getting content from you know older or later dates that's how you would do it now the other interesting thing here this gets a little confusing sometimes is what if you want to go backwards what if we want to go backwards for months what we would do there is we say dollar sign Pig we'd say add month just the same way again but this time we're going to do a minus four so then that takes us back for months and you can do ad months at day at hours out of minutes seconds you know all those are options available to you so now that you kind of manipulate the object and maybe you need to store that right so you might just say dollar sign cow equals you know dollar sign Pig add months - four and so then now the dollar sign Cal variable would have that December 12th date in it so sometimes when you're especially you're first learning to write scripts it's easier just to kind of get that all set ahead of time in a variable and then use that Cal variable instead of using the line pea CAD month - for speaking of scripts the other thing to keep in mind I like to use the variable it makes it easier for you guys understand what's going on but I can also do something like this I need to get date ad months we'll just I have two months to it and so that gives me two months from today so right is what it did was it said I get date so return that value that's what the first parentheses did and then based on that object that came out of there ad months use its method and add 2 to it so another way for you to work with the same information and so speaking of working this information one of things I use this for us like when I'm working the event logs right so I might say get event log and then system log and then I would say after and it will do a get date add minutes and we will subtract a thousand like that so right get the event log system all the events from there that occurred after so in the last thousand minutes so hey dinner and so you can see that whoa I don't know what I did my computer is clearly not happy but God returned all of those events for the last thousand minutes of course I rented a demo there was almost nothing in there so something might be broke as you look at that but not right now okay so that's one of the way so I'll clear that off one more thing when it comes to variables one of the things you can do here someone make a new variable so let me say new date equals and then let's see six three two thousand at 5:00 p.m. oh I need to put that in quotes though so do this and this in height in earth and so then now we have new date 6:35 p.m. right so that feels good you're like hey that's exactly what I wanted but it's actually not right if you do new date and then say get member scroll back to the top what happened all you did was you stored that date as a system string so it's not actually a date object it's just a string and that's not exactly what you looking for so instead just look at a better way of doing that so a square screen again and so we'll do dollar sign better date of course right equals o that's not an equal sign that is and so what you do here is you do get date and then you put all of that same information in here so 6 10 2005 0-0 p.m. just like that so that stores in there if we type in better date oh it's Saturday June 10th right that's our hint that if we do now better date and get member we scroll back to the top so now it's stored in there is a system date time object so now you can do things like add months to long string to short string all of those Commandments that we looked at a minute ago because you just store a specific date and time the one you needed into a variable so keep that in mind also if you want to create the variable as a date object you need to do get date to do that the next thing we're going cat is formatting those dates so what we're going to do here is going to clear screen again so we're going to say get date dollar sign Pig right so get our days it's already stored in there pig but this time we're going to format it now we're going to a main to the formatting features I'm going to do for MS so that just returns the long month of April right that was one of things we want to see earlier if you do short mm well that's oh four and if you do three mmm that's APR right the abbreviation for April so there's a lot of these different formatting options right so that's a great one you could also do things like D right for the date or DD that's the day so you have some different options there or one two three four four DS for the Wednesday so I don't expect you to learn all this right now or memorize any of it if you look down in the description below I'll have a link to a TechNet article that talks about all these different formatting parameters so all the different M's and DS and that type of stuff that you need to know but it is worth knowing you can get a little crazier it's like we could do something like this we could go 1 2 3 4 ms space 2 DS comma and then 4 wise close that so April oh let's try that again 1 2 3 4 see I'm getting right on the first try April 12 2017 so and this is what I'm saying I wanted to point out to you I actually made a mistake but I didn't mean to make a mistake but I plan to make a mistake so who knows anyway is that these letters are case-sensitive so big M's versus little M's big Wiser's too little wise they are different things so make sure when you're playing these formatting options that you don't let the case sensitivity mess with you right because well ms are minutes for example so just keep that in mind as you go forward and then I think you know just one other example I had here because I end up using this one a lot is my Y so for 1217 right very compact little date as it is all about what format is the commandments you're working with need the day Dan or if you're creating log files right for example I used to automate a lot of this stuff by creating CSV files and the CSV files you know my boss was paying the butt and he wanted it to be written as April 12 comma 2017 he couldn't handle being four twelve seventeen so just a matter of working through the parameters you need the nice thing is is that get date function has all these different formatting options get date also has one other four set of formatting options and that is going to be a get date dollar sign Pig and it is called the U format for a UNIX format sorry and what you'll see there is you can do things like Julian dates so if you do % j that tells you that for 1217 this is a hundred and second day of the year so there's those type of things I believe if we do the same thing but we change that to a V let's try a Y between 10 to why not a V that is going to be the week so it is a 17th week of the year so other options available to you I don't do a lot with you format stuff clearly but it is another thing that's available in your tool belt as you're kind of building out these capabilities so hopefully that helps let's clear screen off and the last thing I want to show you is actually the whole reason I made this video series and that is the fact that you can work with dates with things like file system objects aka files so let's switch over to my demos folder the demos and we do a dir I got one file in there perfect and so what I'm going to do here is I'm going to say dollar sign zebra equals get item expense the PDF or sine zebra alright so that's stored in there and then I'm going to do my good old friend select start I do a lot of select stars in my life the reason I did that is I want you guys to see that there's several different things here properties about a file and when I uncover the files today but the things I'm curious about are things like creation time so creation time was December 11 last access time was April 11th and last write time was August of 2013 who knows who cares but what I find myself doing a lot of times where I need to move files from point A to point B especially in my case when I'm moving things from on-prem to SharePoint Online I need to manipulate or query this date objects I just kind of want to show you guys how you would do that so you kinda had an understanding of it because in a later video I'm going to show how to move it to SharePoint Online and then reset the create date the same value that was here instead of the day you moved over the file and this is the basis of that what we're going to do is we're going to type in dollar sign zebra and they're going to do creation time to dinner so you can see that that's a date/time value right we recognize that pretty well but what are we going to do and I'm going to say dollar sign zebra creation time equals dollar sign pig dots what do you want to do you want add months with add months add months and we'll subtract nine so that way I can be shocked as well so boom so that's that just changed the value of that file so now for two dollar sign zebra creation time we can see that the files creation time is Tuesday July 12 2016 pretty simple right but kind of interesting to understand because we can just edit creation time access time and write time and that gives us a lot of power we start moving files rearranging things because now we know with PowerShell we can script resetting all the times or we move the file you know you could use something like robocopy to move the file and take these parameters with you but sometimes you just want to fix them and so PowerShell kind of opens the door for that so hopefully that helps you guys with date and time objects gives you a little context of how this all works and is a just good plain old-fashioned fun so if you ever need any help today it is good old bold divas write we write PowerShell scripts for people we run PowerShell training classes we got a couple coming up soon and then we'll add some more to the schedule always so you can always reach out to me at Shane's cows or Shane Young at bold Ziva's calm happy to help you so also check out the next videos in the series they'll pop up here on you so thanks and have a great day
Info
Channel: Shane Young
Views: 58,779
Rating: undefined out of 5
Keywords: PowerShell, Shane Young, Bold Zebras, PowerShell Training, Learn PowerShell, PowerShell Date, PowerShell Time, PowerShell DateTime, Get-Date, PowerShell format date, Powershell format time, ToShortDateString, ToLongDateString, ToShortTimeString, ToLongTimeString, get-eventlog
Id: YQn7zgwlG40
Channel Id: undefined
Length: 16min 45sec (1005 seconds)
Published: Thu Apr 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.