Try this:
$sourcePath = "C:\Temp\*.txt"
$destFile = "C:\Temp\all.txt"
$encoding = "ASCII"
Remove-Item $destFile -ErrorAction SilentlyContinue
(Get-ChildItem -path $sourcePath) |
where-object { -not $_.PSIsContainer } |
ForEach-Object {
Get-Content $_; ""
} |
Out-File $destFile -Encoding $encoding
Not must to say. The get-childitem
is in parens to ensure the list of files is gathered before all.txt
is created. Directories are then filtered out with where-object { -not $_.PSIsContainer }
and the each file is iterated over. The iterator outputs the file content followed by a blank line. Finally, the whole pipeline is re-directed to $destFile
with the specified encoding.