#PowershellBasics: Select the columns in the output.

Leave a comment

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

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.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,614 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: