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

June 2025

S M T W T F S
1234567
8910 1112 1314
15161718192021
222324 25 262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Tuesday, July 1st, 2025 07:26 am
Powered by Dreamwidth Studios