Date: 2017-11-20 08:22 am (UTC)From: [personal profile] andrewducker
andrewducker: (Default)
Dead simple.

$myfile = get-content C:\tools\testfile.txt -Raw

(-raw means you get it as one string rather than an array of strings - useful if the regex is going to operate across lines. If it's only going to operate on one line at a time then leave that off)

$myfile -replace "thing","otherthing"
will then do your regex. Replace "thing" with your search regex and "otherthing" with what you want it replaced with.

That'll spit the results out to the command line. When you're happy with it, write it back out to a new file with:

$file -replace "thing","otherthing" | Out-File mynewfile.txt -Encoding utf8

(If you leave off the encoding you'll get utf16 by default. Nobody wants that.)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

July 2025

S M T W T F S
  1234 5
6 789101112
13 14 1516171819
20212223242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Wednesday, July 16th, 2025 05:30 am
Powered by Dreamwidth Studios