Menu

PowerShell Get-Content vs Get-Content -Raw: The Real Difference (and Why Your -replace Misses Line Breaks)
📰

PowerShell Get-Content vs Get-Content -Raw: The Real Difference (and Why Your -replace Misses Line Breaks)

DEV Community: beginners·arnostorg·14 days ago
#hAfXijW6
#devtobeginners#devto#dev#line#enter
Reading 0:00
15s threshold

You write a perfectly good regex, run it against a config file with -replace , and nothing changes. No error, no warning, just the same file back. Nine times out of ten the regex is fine. The problem is that Get-Content didn't hand you a file, it handed you a list of lines.

This post is about the single switch that changes that: -Raw . What it actually returns, when you need it, and the two or three places where it quietly bites you.

Everything below was tested in PowerShell 7.4 (7.4.6), and every output block is pasted from a real run.

The setup

A tiny config file to play with:

Set-Content -Path . \app.conf -Value @( 'server=web01' 'port=8080' '# old block' 'debug=true' 'verbose=true' )

Enter fullscreen mode

Exit fullscreen mode

The real difference: many strings vs one string

$lines = Get-Content -Path . \app.conf $whole = Get-Content -Path . \app.conf -Raw

$lines . GetType () . Name $lines . Count $whole . GetType () . Name $whole .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.