November 4, 2021 by Kenneth Fisher
After my post last week on sorting the list, I now have a list of files, sorted in the right order, but now I want to pull back just the name column. Remember, I couldn’t restrict it to just that from get-childitem because I needed the LastWriteTime column to sort on. Not to mention I couldn’t find a way to restrict the columns to anything other than all of them or just name.
$Path = "C:\temp\" get-childitem -path $Path -filter *.csv | sort-object -property LastWriteTime | select-object name, length
The select-object function lets you pull a list of properties from the input, among other things. One cool thing I noticed while researching this post is that it has -first, -last, -skip and -unique properties. -unique is basically a distinct. Not overly useful here, but good to know. -first is like the TOP clause in a SELECT. Pass in the number of rows you want back from the top.
$Path = "C:\temp\" get-childitem -path $Path -filter *.csv | sort-object -property LastWriteTime | select-object name -first 2
You can then add in the -skip parameter to do pagination just like you would do by using OFFSET/FETCH in a SQL query. Although there is no ORDER BY requirement so if you want to be sure of your order, make sure you sort the input using sort-order. Last but not least, is .. -last. Which is the opposite of -first and gives you the last x rows of the input.