3

I can print directory contents (Windows 10) with Mode, LastWriteTime etc included but I want to use something like dir > test.csv to only print the Name of each file. Is there such a way to do this?

1
  • 2
    (Ls "$Env:UserProfile\Desktop\New Folder" -File -Fo).Name > file.csv. Please note that your CSV will probably be encoded as UTF-16 with the little-endian byte order. If you want a different encoding type, UTF8 or ASCII for instance, do it like this instead, Get-ChildItem -Path "$Env:UserProfile\Desktop\New Folder" -File -Force | Select-Object -ExpandProperty Name | Set-Content -Encoding UTF8 -Path ".\file.csv", or shortened Ls "$Env:UserProfile\Desktop\New Folder" -File -Fo|Select -Exp Name|SC -En ASCII file.csv Commented Oct 15, 2020 at 17:52

3 Answers 3

5

Try like this:

(dir "PATH" -file | select name) >file.csv

Basename only returns the file name of the item. To filter only files add the -File switch with dir and to get recursively add -Recurse. You can add other properties comma separated, too.

Sign up to request clarification or add additional context in comments.

5 Comments

So this works but I need file extensions and don't need mode nor LastWriteTime.
Modified as per your need
Thank you but now I'm getting a blank CSV file? I used: (dir "C:\Users\NAME.OFF\desktop\New Folder" -file | select name) > file.csv
@adrift - confirm that your target dir has files in it. the code you show SHOULD work if there is at least one file in the target dir ... [grin]
I am also getting a blank csv file, when I execute it without the > file.csv then it shows me all filenames correct, but with the > file.csv iti produces a blank file. Did you ever resolved this ?
0

A bit more idiomatic way to do this:

Get-ChildItem | Select-Object -Property Name | Export-Csv "files.csv"

If you need to do get a list from another directory add -Path:

Get-ChildItem -Path $path

Comments

-2

My post is based on solution provided by @wasif in this same thread.

Removing the Brackets worked for me.

Try like this:

dir "PATH" -file | select name > file.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.