Add first and last line to multiple text files in a directory by powershell

How can i add first and last line to multiple files in a single directory by powershell?

Example: The directory is "Indexes".. It has the files like text files 1 to text files 100. Now, i want to add first line and last line to all those text files 1 to text files 100.

For a single text file i was able to do by,
$File = get-content 'path'
$firstline = 'info you would like to write in first line'
$lastline = 'info you would like to write in line line'
set-content 'path' - value $firstline, $file, $lastline


But for multiple files i was not able to do. Please help me with this. Thanks in advance.
sam bAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
You just run it in a loop over the files:
$Path = "C:\Temp\*.txt"
$FirstLine = 'info you would like to write in first line'
$LastLine = 'info you would like to write in last line'
Get-ChildItem -Path $Path | ForEach-Object {
	$File = Get-Content $_.FullName
	Set-Content -Path $_.FullName -Value $FirstLine, $File, $LastLine
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sam bAuthor Commented:
Firstly, sorry for the late response.
Thanks oBdA.

Your script helped me to a lot for my task.
Keeping your script as base idea, i cam up with my script.

Thanks alot for your help. :)
0
oBdACommented:
Question answered.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.