PowerShell - how to create list of files in directory

Thuleman

Supreme [H]ardness
Joined
Apr 13, 2004
Messages
5,833
In the words of the famous Liz Lemon: What the what?!

I am getting reasonably frustrated by being unable to do what seems to be a simple thing, all I want is a script that goes through a directory and creates an output file which has the names of all files with a certain extension, one file per line, and no other information. How hard can that be?

Here's what I have:
Code:
$File = "E:\temp\file-list.txt"
get-childitem "E:\temp" -recurse -include *.dat |
ForEach-Object { $_ | format-table name -hideTableHeaders | Out-File $file -append }

This creates output that looks like this:

[CR][LF]
filename.dat
[CR][LF]
[CR][LF]
[CR][LF]
filename.dat
[CR][LF]
[CR][LF]
[CR][LF]
filename.dat
etc...

What the what?!?!!!?!
Where do all those new-lines come from?

format-list output isn't too much different.

I guess I am just approaching this the wrong way but googling around for it just results in information overload ( Los Links! )

So, is there a way to do this in PowerShell? Thanks in advance for any insight you may be able to provide.
 
I don't know about powershell but this might help:

dir c:\windows\*.log /b > c:test.txt

It certainly does! Thanks. I knew of dir I was just so focused on wanting to do this in PS that I didn't even think about it. ;)

It would still be nice to figure out how to do just that in PS.
 
Try this:

Code:
get-childitem -include *.dat -recurse | format-table Name -hidetableheaders | out-file $file

There's no need for the foreach {} in there since format-table will take a collection as pipeline input.
 
Doesn't seem to work.

Code:
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At line:1 char:87
+ get-childitem -include *.pdf -recurse | format-table Name -hidetableheaders | out-file <<<<  $file
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileComm
   and
 
Doesn't seem to work.

Code:
Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At line:1 char:87
+ get-childitem -include *.pdf -recurse | format-table Name -hidetableheaders | out-file <<<<  $file
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileComm
   and

$file wasn't defined/ null

Try the code below, that should dump a text file with a list of pdf's in the root of the current directory.

Code:
> Get-ChildItem -Recurse -Include *.pdf| Format-Table name -hidetableheaders | Out-File 'pdf list.txt'
 
Last edited:
Oh, yeah. Sorry, I left $file on the end assuming you'd use that variable you defined in your original script.

Like Wiseguy said, change $file to a path and it'll work.
 
Back
Top