W2KSG: Folders Creation date

WMI has a class Win32_Directory for working with folders on a machine. We can use this class to find all folders created this month.

Listing 6.22

$d = "’" + (Get-WmiObject -Class Win32_Directory -Filter "name=’c:\’").ConvertFromdateTime([datetime]"09/01/2008") + "’"
Get-WmiObject -Class Win32_Directory -Filter "CreationDate > $d" | Select Name, CreationDate

We start by creating a date in WMI format.  We use the Win32_Directory WMI class and filter on the root folder for speed. Note that we have to use \ instaed of when dealing with file paths in WMI.  We then use the ConvertFromDateTime method to put the required date into WMI format.  The date will look like this


Note that we have put ‘ ‘ round the date.  This is needed for the correct formatting of the WMI query.

We then use Win32_Directory again and filter based on dates greater than our test date i.e. folders created this month.

The really great thing about this is that we could use -computername to specify a remote machine. We could modify the script to read a file of machine names and test our servers for new folders.


Share this post :


Technorati Tags: ,


About these ads
This entry was posted in PowerShell and WMI. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s