Hopefully Simple Question About LastWriteTime

by chuckstaburnt at 2013-03-21 06:36:32

Hello,

What I’m trying to do, but am running into difficulty with, is finding directory with the LastWriteTime, ignoring any files which may be in that same directory.

For instance:
I have a directory C:\folder and in that folder is FolderA, FolderB, FolderC, FileA, and FileB. I don’t care about FileA or FileB and I want to exclude them from the seatch. Currently, This is the line in question.
$LatestDirectory = GCI -path "C:\folder" | sort LastWriteTime | select -last 1 -expandproperty name

If FileA is newer than FolderA, B, or C, then $LatestDirectory = FileA.

Please help.I’m guessing this is rather simple and I’m overlooking something. Thank you.
by Klaas at 2013-03-21 08:17:48
Try adding the -directory switch:
$LatestDirectory = GCI -path "C:\folder" -directory | sort LastWriteTime | select -last 1 -expandproperty name
by chuckstaburnt at 2013-03-21 10:29:03
When I tried that, I got the following error:

Get-ChildItem : A parameter cannot be found that matched parameter name ‘directory’.
by Nobody at 2013-03-21 10:55:16
This should do the trick.
$LatestDirectory = GCI -path "C:\folder" | where-object {$.psiscontainer}|sort LastWriteTime | select -last 1 -expandproperty name
by chuckstaburnt at 2013-03-21 16:33:04
That appears to have done the trick! Thank you. Would you mind telling me what $.psiscontainer means? Are there other objects identified by something like this?
by Lembasts at 2013-03-21 17:22:00
psicontainer is true if the object is a folder and false if it is a file.
The reason the -directory switch didnt work is because I think it requires powershell v3. Probably a good idea to get and install that anyway.