Blog Post

#PowershellBasics: Get a list of files from a directory.

,

I’ve got a project I’m working on at the moment and it includes a number of elements from my recent Powershell Basics posts. If it works out I’ll share later but feel free to guess. ?? In the mean time the next element I needed was a list of files in given directory.

$Path = "C:temp"
get-childitem -path $Path -filter *.csv

Directory: C:temp

Mode LastWriteTime Length Name

—- ————- —— —-

-a—- 6/27/2021 11:26 AM 66 Employee.csv

-a—- 7/12/2021 5:15 PM 245609 comments.csv

-a—- 7/30/2021 7:04 PM 62443 test1.csv

I’m using the command get-childitem but I could just have easily used it’s standard alias dir. It has a number of parameters but here I’m passing in the -path to tell it where to look and I’m using the -filter parameter to just get the CSV files. I could just as easily using -include to get the CSVs, or use -exclude to remove the files I’m not interested in. Last but not least, in my case I only want to get the file names so I’m going to include the -name parameter.

$Path = "C:temp"
get-childitem -path $Path -filter *.csv -name

Employee.csv

comments.csv

test1.csv

Note: With the -name parameter I don’t get a header either, just the list of file names. Sadly there is no way to sort here, so I’ll have to figure that out next.

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

Rate

5 (1)

Share

Share

Rate

5 (1)