Reputation: 5988
I would like to copy a file from one location to another, and replace the first line of text with a string. I almost have the script complete, but not quite there.. (see below)
# -- copy the ASCX file to the control templates
$fileName = "LandingUserControl.ascx"
$source = "D:\TfsProjects\LandingPage\" + $fileName
$dest = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\LandingPage"
#copy-item $source $dest -Force
# -- Replace first line with assembly name
$destf = $dest + "\" + $fileName
$destfTemp = $destf + ".temp"
Get-Content $destf | select -skip 1 | "<text to add>" + $_) | Set-Content $destfTemp
Upvotes: 11
Views: 19296
Reputation: 5717
The accepted answer did not work for me ("file already in use"). I fixed it like this:
$MyContent = .{
"<text to add>"
Get-Content test1.txt | Select-Object -Skip 1
}
Set-Content -Path "test2.txt" -Value $MyContent
Upvotes: 0
Reputation: 496
In the "more than one way to skin a cat" vein you could accomplish the same thing with Out-File, if that's your preference on Thursdays. Written for better understanding the flow versus one-line condensation.
$x = Get-Content $source_file
$x[0] = "stuff you want to put on first line"
$x | Out-File $dest_file
This uses the fact that Get-Content creates an array, with each line being an element of that array.
Upvotes: 13
Reputation: 42035
Not a one-liner but it works (replace test1.txt and test2.txt with your paths):
.{
"<text to add>"
Get-Content test1.txt | Select-Object -Skip 1
} |
Set-Content test2.txt
Upvotes: 16