1

I'm trying to find resources to help me write a powershell script to add text to an existing aspx file but can't seem to find anything. Does anybody have any suggestions or is there anybody that can help me some other way?

Update: So it's out there, I'm using version 1.0.

I have:

$lines = Get-Content foo.aspx

$lines = "<head>asfdsfsafd</head>" + $lines

$lines | Out-File "c:\documents and settings\...\foo.aspx" -Encoding utf8 

but it wipes out everything originally in foo.aspx and creates <head>asfdsfsafd</head><head>asfdsfsafd</head>. How do I fix it so it keeps the original stuff in foo and add stuff to the beginning of the file rather than end?

Update:

I've figured out how to add text with:

$lines = add-content -path "C:\Documents and Settings\..\foo.aspx" -value "Warning...."

but want the text to go at the beginning of the aspx file

Update: I found a function that does what I want and I'm all set.

1
  • That is weird. Why do you want this? Could you explain it? Wouldn't be more easy to make an aspx file that reads text from a database and create something (ideally another aspx) that updates text in DB? Commented May 15, 2014 at 14:40

1 Answer 1

1

You can get the content (array of strings) of the ASPX file using Get-Content:

$lines = Get-Content foo.aspx

Or you can get the content as a single string which is sometimes more useful if you want to use a regex that spans lines:

$content = Get-Content foo.aspx -raw

As far as changing the content, you have all sorts of options:

$content = "text before " + $content
$content += "text after"
$content = $content -replace 'regex pattern','replacement text'

And then to write back out to the file:

$content | Out-File foo.aspx -Encoding <UTF8 or ASCII or UNICODE>
Sign up to request clarification or add additional context in comments.

10 Comments

When I run $lines = Get-Content foo.aspx it gives me everything, as you said. However, when I run $content = Get-Content foo.aspx -raw I get Get-Content : A parameter cannot be found that matches parameter name 'raw'. At C:\....
I think I found my answer and it's because raw is for a different version than what I'm using. I'm using version 1.0. Is there a command I can use for this version that is equivalent?
1.0 really? :-) You can use $content = [io.file]::ReadAllText("$pwd\foo.aspx").
That works but it does what Get-Content foo.aspx does and gives me everything. It's not spanning lines?
Yeah there was a quote missing - fixed. And yeah, replacement text replaces what is specified by regex pattern. WRT single string object, yes every line, separated by CRLF chars, is put into a single string. That is necessary if you have regex that matches text across separate lines.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.