PowerShell pitfalls: reading text from file using get-content

I had a really strange effect in PowerShell that puzzled me for hours!

I have the following script:

$a = @'
One
Two
'@
$a
$p = [regex]"One"
$p.Replace($a, "OneReplaced")

$b = get-content -path templ.txt
$b
$q = [regex]"One"
$q.Replace($b, "OneReplaced")

And a file templ.txt containing the following text:

One
Two

When I execute the script I get the following output:

One
Two
OneReplaced
Two
One
Two
OneReplaced Two

So what happens:

I initialize a variable $a with two lines of text: line 1: One, line 2: Two. When I display variable $a it shows One and Two on two seperate lines. I now replace One with OneReplaced. Output of the replacement is two lines of text. Line 1: OneReplaced, line 2: Two.

Everything ok so far.

I now read the contents of variable $b from the file templ.txt. This file contains two lines of text: line 1: One, line 2: Two. When I display variable $b it shows One and Two on two seperate lines. I now replace One with OneReplaced. Output of the replacement is ONE LINE of text: OneReplaced Two.

This is not what I expected.

After a lot of debugging I found out why this happened. When you do $b = get-content -path templ.txt you don't get a string back, but an object array. You can see that when you do: (get-content -path templ.txt).GetType(), this displays:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

If you inspect the Object[] variable $b, you see that $b[0] = "One" and $b[1] = "Two".

When the command $q.Replace($b, "OneReplaced") is executed, the variable $b of type Object[] is cast to a string. This cast combines the string objects in the Object[] by appending them with a space in the middle.

So what is the simple solution to all this: when reading the content, join all lines with a newline, as in the following code line:

$b = [string]::join([environment]::newline, (get-content -path templ.txt))

Such a pity that this costed me 4 hours, I thought it was in the regular expression replacement:-(

But the good thing is that I can now solve that nasty bug in my Template Engine using PowerShell expressions.

Published Sunday, December 31, 2006 2:06 AM by svdoever
Filed under: ,

Comments

Friday, February 02, 2007 7:00 AM by Tim Hardy

# re: PowerShell pitfalls: reading text from file using get-content

Many thanks for this. I've hit your blog several times during my first two days of using PowerShell and found it very helpful.

Thursday, February 22, 2007 8:02 AM by Arjan

# re: PowerShell pitfalls: reading text from file using get-content

Heh, net wat ik nodig had :)

Thursday, February 22, 2007 8:05 AM by svdoever

# re: PowerShell pitfalls: reading text from file using get-content

He Arjan, aan het Powershellen!

Thursday, January 10, 2008 11:39 AM by Evgeny Tugarev

# re: PowerShell pitfalls: reading text from file using get-content

Thanks a lot for this post! I faced the same problem with get-content and spent several hours trying to figure it out :/

Wednesday, January 23, 2008 5:07 AM by ray

# re: PowerShell pitfalls: reading text from file using get-content

Thanks it helps alot.

Leave a Comment

(required) 
(required) 
(optional)
(required)