copy data from multiple text files with powershell

Hi EE

I am needing to pull three lines from 100s of text files and export to one file .

I have been testing different variations with these and can get it to output from one file but I cant get it to read each source file and export to one file .. can someone assist ?

#This works
Get-Content -Path C:\TESTING\Log.log | Select-Object -Index 4,5,6 | Out-File out_test.txt

#This works
$logfile = Get-Content -Path C:\TESTING\Log.log
$logfile[4,5,6] | Out-File out_test.txt

How to I have it check the directory of the source files and copy those three lines to one file ?  
Thank you all , appreciate the help here always .
LVL 2
MilesLoganAsked:
Who is Participating?
 
NorieVBA ExpertCommented:
Try this.
$folder ="C:\test\TESTING\"
$files = Get-ChildItem $folder

foreach ($file in $files)
{
Get-Content -Path $file.FullName | Select-Object -Index 4,5,6 | Out-File -Append C:\test\out_test.txt
}

Open in new window

0
 
NorieVBA ExpertCommented:
Does the directory you want to extract the 3 lines from only contain files that you want to extract data from or does it have other files?
0
 
MilesLoganAuthor Commented:
Only the files I need to extract data from
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
oBdACommented:
This sill give you the file names as well:
Get-ChildItem -Path "C:\test\TESTING\*.log" | ForEach-Object {
	$Name = $_.Name
	Get-Content $_.FullName -TotalCount 6 | Select-Object -Skip 3 -Property @{n='Name'; e={$Name}}, @{n='Line'; e={$_}}
} | Export-Csv -NoTypeInformation -Path out_test.csv

Open in new window

2
 
MilesLoganAuthor Commented:
That worked perfect ! I was close lol ... I appreciate the quick assistance ..
0
 
MilesLoganAuthor Commented:
sorry I didnt see your responce oBdA .. that did the trick also ..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.