Hey, Scripting Guy! Weekend Scripter: The Scripting Wife Works with Dates in Windows PowerShell

Bookmark and Share


Microsoft Scripting Guy Ed Wilson here. I love getting up early on Saturday morning and spending an hour or so on breakfast. I boil a pot of fresh spring water, carefully pour it into my large ceramic teapot, measure out English Breakfast tea leaves, some lemon grass, and a cinnamon stick, and set it aside to steep for four minutes. In the meantime, I boil a pot of water, and carefully pour in a measure of Irish steel-cut oats and simmer and stir for 30 minutes. In between stirring the oats, I wash organic blueberries and place them in the bottom of my bowl. After the steel cut oats have finished simmering, I spoon them onto the top of the blueberries and sprinkle some fresh ground cinnamon on top. That is it. No butter, no sugar is needed—nothing but natural whole grain oats, fresh fruit, and real cinnamon. The steel cut oats have a wonderful nutty flavor to them, and the blueberry and cinnamon complement the flavor in the perfect manner.

I take my bowl and my cup outside to the swing on the front porch and enjoy the cool spring morning as the weekend takes its time awakening to the start of a new day. The Scripting Wife will not be seen for hours, the neighbors are still asleep, and it is the perfect time for contemplation. I close my eyes, and contemplate the state of the 2010 Scripting Games—wondering if we have left out anything.

“Hey Scripting Guy! I have a question for you,” the Scripting Wife said as she interrupted my contemplative pose.

“Well, good morning my peach,” I cheerily replied. “What can I possibly do for one as bright and self-sufficient as you?”

“I just realized our summer vacation is rapidly approaching, and if we don’t get our act together, it will be a fiasco trying to get everything rounded up and ready to go. I do not want to stay up until 4:00 A.M. like we did before we went on vacation in the spring,” she exclaimed.

“And your point is…,” I said dripping with sincerity.

“I need to know how many days between now and when we leave to go to the Smokey Mountains.”

“You have a calendar on your computer. Open it and count the days. Use your finger if you have to,” I offered helpfully.

“I could use your finger after I break it off,” she suggested, disbelieving my sincerity, “but I would rather use Windows PowerShell if I am supposed to be learning this stuff.”

Now she had my undivided attention. “Go get your laptop. You are going to love this,” I said.

As the Scripting Wife sat on the porch swing with her laptop cradled between us, I asked her to open the Windows PowerShell console.

“Type Get-Date and press ENTER. Now tell me what it does,” I said.

“It displays the current day, date, and time,” she replied. “So what good is that? I know what day and time it is. Dude, you men are all alike sometimes.”

“Cute and lovable?” I hoped.

“Obtuse. So are you going to tell me how to figure out how many days before we go on vacation or not?” she asked.

“Okay. Let’s create a date in the future. Type Get-D and press TAB. That will expand the Get-Date cmdlet. Now type –D and press TAB. That will expand the –Date parameter. Let’s use June 1, 2010, as the date for our practice session. You can substitute it on your own later. To type June 1, 2010, it is easiest to type 6/1/2010 and press ENTER. After you have done that, slide your monitor over so I can take a look at it,” I said.

“Okay. This looks right to me,” she said, showing me what you see in the following image.

Note: Our porch session was later re-enacted back in the office in order to produce the screen shots. This means the dates indicate Wednesday, instead of Saturday.

Image of using Get-Date cmdlet


“Now let’s subtract the two dates. Press the up arrow to get your Get-Date -Date 6/1/2010 command. Now before you do anything, press the HOME key to move to the beginning of the line. When you are at the beginning of the line, type a beginning parenthesis, press the END key to move to the end of the command line, and then type an ending parenthesis. Your command now looks like (Get-Date -Date 6/1/2010). Because you are at the end of the line, you can go ahead and type the minus sign and the Get-Date command inside a set of parentheses. Your completed command should look like (Get-Date -Date 6/1/2010) - (Get-Date). After you have it typed, let me see it please,” I said.

“Okay, I think I have it,” she said.

“Then press ENTER to run the command, and show me what you get back,” I offered.

“Here you go,” she said, showing me what’s shown in the following image.

Image of showing time with 10 different measurements


“That is pretty cool,” she admitted. “How does that work?”

“Remember when I said everything is an object?” I asked.

“Yes, you say it all the time,” she sighed.

“The Get-Date cmdlet returns a DateTime object. You can see this by piping the Get-Date cmdlet to the Get-Member cmdlet. Why don’t you go ahead and do that,” I suggested.

“Okay. Here, take a look,” she said. “But I do not see where that tells me anything.” (The following image shows you what she showed me.)

Image of piping the Get-Date cmdlet to the Get-Member cmdlet


“Let’s take it one more level. Because we know we are working with a datetime object, we can see if we can use that object directly. To check for this, put square brackets around the word datetime, and press ENTER to see what happens,” I said.

“It comes back with a bunch of junk. Here look,” she said, showing me what you see in the following image.

Image of checking datetime object


“That’s not junk,” I said.

“It looks like it to me,” she said.

“Did it give you an error?” I nudged.

“No,” she said.

“Then it told you that was a legitimate command,” I revealed. “Now let’s use that information to our advantage.”

“Okay. But you are starting to get geeky on me, and when you do that, you just make me mad,” she cautioned.

“Press the up arrow to retrieve your [datetime] command, and immediately type the date 6/1/2010 inside a set of quotation marks by typing [datetime]“6/1/2010” and surround the entire thing with a set of parentheses. At the end of the command, add a period, and type the command getType(). Your command is therefore ([datetime]"6/1/2010").gettype(). Press ENTER to run it,” I said.

“Is this right?” she asked, showing me what you see in the following image.

Image of using getType with [datetime] command


“Absolutely perfect,” I effused.

“So. I am not seeing where this is useful,” she said matter-of-factly.

“Because we can create a datetime object from the command line by using [datetime] and because we create a datetime object by using the Get-Date cmdlet, it means we are working with the same object. This means we can rewrite our command by typing ([datetime]"6/1/2010") - (get-date),” I said.

“Oh, I get it. It is shorter, and therefore less typing. Is this right?” she asked, showing what you see in the following image.

Image of a shortened command


“Cool, you got it. I see it took you a few tries. What happened?” I asked.

“Well, you were distracting me by talking, and I got the quotes in the wrong place. Luckily, I used the up arrow and did not have to type the whole command over again,” she said.

“I am sorry. I did not mean to distract you,” I apologized.

“That’s okay. It gave me a chance to practice using the command line. This is pretty cool. Thank you for showing it to me,” she said.

“No problem. Working with dates in Windows PowerShell is important. It is also probably important for the 2010 Scripting Games,” I said.

“I am getting worried about that,” she said.

“Have you looked at the study guide?” I asked.


“Then you will do fine. I promise.”

“I had better. Or you will be in trouble big time,” she said as she playfully smacked me on the shoulder. At least, I think it was playful. It stung a bit.


If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson and Craig Liebendorfer, Scripting Guys


Comments (3)

Cancel reply

  1. @spsp

    No she is not dumb at all…But she is a complete newbie, and it is safer to say up arrow and press enter. Not everyone knows that they can retrieve the last command by pressing up arrow. In fact, if you thinking about, most Windows users who begin using Windows since 1995 have had very little experience with DOS, or any other command shell. In addition, the Scripting Wife is very careful not to get "click happy" when I am showing her a new technique. Therefore, just because I do not need to say up arrow, press enter, she will not press enter until it is mentioned, in case I want her to edit the line prior to executing the command. She is a GREAT student in that regard.

  2. spsp says:

    good stuff.. though i wish you dont have to specify up-arrow and enter etc.. im sure u're wife aint that dumb.

  3. Charlie Spencer says:

    Anybody still looking / monitoring this ancient topic?

    I have no previous scripting or object experience. I got hung in a couple of places. First:

    “Because we know we are working with a datetime object, we can see if we can use that object directly. ”

    What does it mean to use an object ‘directly’? The sentence seems to imply there are other ways to use an object besides directly. What are they?

    The next place I got bogged down was:


    I don’t understand the syntax. I assume since there’s no Verb-Noun that this isn’t a cmdlet. So what is it? I’m not learning anything I can apply elsewhere. The implication seems to be that things besides cmdlets can be run in PowerShell. What are those other things?

    I’m fine with the syntax of cmdlets. Get-Help does a fine job explaining them. Where I’ve regularly gotten lost / frustrated / abandoned in my PS exploration is when non-cmdlet commands start appearing.

Skip to main content