Contents

Going on a Get-Date…part 1


Contents

Simple issues, simple solutions?

Recently I’ve been doing quite some break-fix cases, which didn’t quite make blogging a high priority.

It did on occasion provide me with nice little gems that I decided to spend a little personal time on, because I thought it would be better to force myself to use PowerShell instead of resolving things manually or through a GUI. Also, I thought these issues would be “simple, quick solutions”, which turned out to be a bit too optimistic.

Looking back, the extra effort spent on these issues will most probably pay me back in the not too long future, because I have the feeling I’ll re-use the concepts again.

Enough chit-chat, on to the issues!

Problem #1

Fairly simple issue really, I had created some temporary Virtual Machines to see if my MDT solution was working properly, and I had removed the Virtual Machines from Hyper-V manager to clean up again.

Unfortunately, this did not remove my Virtual Hard disks, which is actually what I really wanted [because honestly, who cares about the 10kb .XML when you have a 10GB .VHDX gathering dust].

I went to my VHD folder and listed all my disk files:

1
2
Set-Location C:\Hyper-V\VHD
Get-ChildItem -Filter *.vhdx

Great, now I had a list of all the disk files. I however only needed to have a list of the disk files that were created today, because these were the disks for the temporary VM’s

Simple right?

1
Get-ChildItem -Filter *.vhdx | Where-Object {$_.CreationTime -match '26/01/2015'}

Awesome, the files were listed once more. But wait… this is nice, but ideally I wouldn’t have to tell the system what day it is today.

Since I know I can easily get the current date using

1
Get-Date

I thought I was able to solve my “issue” with the following command

1
Get-ChildItem -Filter *.vhdx | Where-Object {$_.CreationTime -match (Get-Date)}

Of course too simple of a thought..  the output from Get-Date looks nothing like the input I need…

I was trying to figure out how I could get it to display the results I needed.

A quick glance at

1
Get-Help Get-Date -Detailed

surely provided me with the way to go!

1
Get-Date -UFormat %D

Unfortunately this didn’t work, even though it gave me the exact same result as me manually entering the date in a string… or did it… It turned out I didn’t read the output properly and instead of 26/1/2016 it gave me 26/01/2016 instead, which is almost like the date I needed, just not quite…at all…

Now believe me when I say that now that I know the solutions that it’s “easy” to fix, but reading properly is basically most of the work…

Solution

Instead of using UFormat, using Format, but with the proper format switch, resolved my issue

Looking at how Get-Date should be used, brought me to the following MSDN link

1
Get-Help Get-Date -Parameter Format

It’s all about the formatting! So, what it all boils down to:

1
Get-ChildItem -Filter *.vhdx | Where-Object {$_.CreationTime -match (Get-Date -Format d)}

Do note that while many things in PowerShell aren’t case sensitive, formatting rules ARE!

But wait, there’s more…

Great tip from your friendly neighbourhood PowerShell-Man also provided me with an alternative solution, which uses the [DateTime] type accelerator to generate a .NET framework System.Globalization.DateTimeFormatInfo object.

1
[DateTime]::Parse('26/01/2016')

While I already found the solution, this tip would come handy quite soon… but more on that in part 2!