tags:

views:

37

answers:

2

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
+2  A: 

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
Roman Kuzmin
what does the "." mean?
BrokeMyLegBiking
The operator "." means "execute the following script block/command in the current context". There is also the operator “ “.” normally works slightly faster.
Roman Kuzmin
Roman Kuzmin
excellent! I'm really loving powershell. thanks for the help
BrokeMyLegBiking
A: 

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.

EdgeVB