Powershell – directory listing export to csv

csvpowershell

I'm trying to pull the names of files from a directory into a csv file with the following:

Get-ChildItem -name | % { [IO.Path]::GetFileNameWithoutExtension($_) } 
                                                  | Export-Csv users.csv

Using '% { [IO.Path]::GetFileNameWithoutExtension($_) }' to display the files with out extensions.

All I get is the following in return:

>>
Length
12
10
13
9
14
18
14
11

Any Ideas?

Best Answer

What does the directory really look like? Guessing the dir does not have dirs named 12,10,13... It does help us help you when we know what you're working with.
Why not just use "basename"?

PS C:\temp\post> ls

    Directory: C:\temp\post


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          3/5/2012   2:55 PM         72 mark.txt
-a---          3/5/2012   2:55 PM         72 mike.txt
-a---          3/5/2012   2:55 PM         72 molly.txt
-a---          3/5/2012   2:57 PM         81 users.csv

PS C:\temp\post> gci | select baseName | export-csv users.csv -noTypeInfo

PS C:\temp\post> gc .\users.csv
"BaseName"
"mark"
"mike"
"molly"
"users"