Blog Post

#PowershellBasics: Select the columns in the output.

,

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

Name Length

—- ——

Employee.csv 66

test1.csv 245609

comments.csv 62443

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

Name

—-

Employee.csv

test1.csv

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.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating