Read a whole file as one string with Get-Content -Raw

Without -Raw, Get-Content splits files into line arrays — a trap that breaks multi-line regex and exact string comparisons.

WindowsFilesscriptingpowershellLow risk
Add -Raw to Get-Content to read the file as a single string with newlines preserved.

01Command

Command
$ $html = Get-Content 'C:\Temp\page.html' -Raw

02What it does

By default Get-Content emits one string object per line, so saving it to a variable gives you an array — and (Get-Content $f) -replace 'a','b' then writes the file back with different line endings. -Raw returns one string with original line endings intact, which is what multi-line regex matching and byte-exact comparisons need. The companion forgotten switch is -Delimiter, e.g. -Delimiter ';' for semicolon files.

03Risk

RiskLow risk
RequirementsPowerShell
!Warning: Reading a multi-GB file with -Raw loads it entirely into memory; for big files prefer streaming or .NET's StreamReader.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • Get-Content | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    Evidence: Microsoft documents the -Raw switch, which ignores line breaks and returns the file content as a single string.
  • Get-Content - PowerShell
    SS64 · technical · 2026-09-17
    Evidence: SS64 explains -Raw returns one string preserving newline characters, versus the default line-by-line array.

Last verified: 2026-09-17 · Category: Files & Folders

06Related solutions