VMware Cloud Community
DZ1
Hot Shot
Hot Shot

Filtering using the lowest number



Even though this question does not contain any directly related PowerCLI cmdlets, the overall script itself is PowerCLI. 


I'm trying to filter a directory, but I only want to get the lowest (Minimum) number.  I had the idea in my head, and I really thought it would be easier than this.


I changed the date on my computer, so when I ran the script it would produce a LastWriteTime that appeared to be a certain year and month, and here is what I want to do.


I have two entries


Mode                LastWriteTime     Length Name


-


               -


    -


-




-a---         10/1/2014   7:30 PM       1555 10-1-2014 Report.csv



-a---         10/3/2014   7:30 PM       1555 10-3-2014 Report.csv 



What I want to do is only filter for the oldest file, the one that is 10/1/2014 



The name of the file is generated by a date, so I need the filter to be dynamic



Assuming I'm already in the correct directory, I tried this:



ls | where { ($_.LastWriteTime | measure -Minimum | select -ExpandProperty Minimum) }



What I thought should happen is that I would retrieve only 1 result and that result would be the file with a date of "10/1/2014", but both files show up, as if I ran an ls on the directory.



To make sure I was thinking logically (or so I thought), 



I tried this "10/1/2014", "10/10/2014", "10/26/2014" | measure -Minimum | select -ExpandProperty Minimum



That works, only "10/1/2014" comes back, but I can't seem to get it to work when trying to filter for a certain file using ls (Get-ChildItem)


0 Kudos
2 Replies
sneddo
Hot Shot
Hot Shot

How about this?

Get-ChildItem | Sort-Object LastWriteTime | Select -First 1

Basically just sorts in ascending order and returns the first value (i.e. the oldest)

0 Kudos
VMwareGuy007
Contributor
Contributor

First, I want to thank you for your help.  Second, I want to thank my brain for making this more complicated than it needed to be.  I don't know how many times I've sorted things that way, but for some reason, I did not think of it in this case.  Well, I need to always remember to keep it simple.  Thanks again. 

0 Kudos