Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:17:25+00:00 2026-05-28T20:17:25+00:00

I’m currently working on some search and replace operation that I’m trying to automate

  • 0

I’m currently working on some search and replace operation that I’m trying to automate using powershell. Unfortunately I recognized yesterday that we’ve different file encodings in our codebase (UTF8 and ASCII). Because we’re doing these search and replace operations in a different branch I can’t change the file encodings at this stage.

If I’m running the following lines it changes all files to UCS-2 Little Eindian even though my default powershell encoding is set to iso-8859-1 (Western European (Windows)).

$content = Get-Content $_.Path
$content -replace 'myOldText' , 'myNewText' | Out-File $_.Path

Is there a way to prevent powershell from changing the file’s encoding?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:17:26+00:00Added an answer on May 28, 2026 at 8:17 pm

    Out-File has a default encoding unless overriden with the -Encoding parameter:

    What I’ve done to solve this is to try to get the original file’s encoding by reading trying to read it’s byte order mark and using it as the-Encoding parameter value.

    Here’s an example processing a bunch of text file paths, getting the original encoding, processing the content and writing it back to file with the original’s encoding.

    function Get-FileEncoding {
        param ( [string] $FilePath )
    
        [byte[]] $byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $FilePath
    
        if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
            { $encoding = 'UTF8' }  
        elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
            { $encoding = 'BigEndianUnicode' }
        elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
             { $encoding = 'Unicode' }
        elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
            { $encoding = 'UTF32' }
        elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76)
            { $encoding = 'UTF7'}
        else
            { $encoding = 'ASCII' }
        return $encoding
    }
    
    foreach ($textFile in $textFiles) {
        $encoding = Get-FileEncoding $textFile
        $content = Get-Content -Encoding $encoding
        # Process content here...
        $content | Set-Content -Path $textFile -Encoding $encoding
    }
    

    Update Here is an example of getting the original file encoding using the StreamReader class. The example reads the first 3 bytes of the file so that the CurrentEncoding property gets set based on the result of its internal BOM detection routine.

    http://msdn.microsoft.com/en-us/library/9y86s1a9.aspx

    The detectEncodingFromByteOrderMarks parameter detects the encoding by
    looking at the first three bytes of the stream. It automatically
    recognizes UTF-8, little-endian Unicode, and big-endian Unicode text
    if the file starts with the appropriate byte order marks. Otherwise,
    the UTF8Encoding is used. See the Encoding.GetPreamble method for more
    information.

    http://msdn.microsoft.com/en-us/library/system.text.encoding.getpreamble.aspx

    $text = @" 
    This is
    my text file
    contents.
    "@
    
    #Create text file.
    [IO.File]::WriteAllText($filePath, $text, [System.Text.Encoding]::BigEndianUnicode)
    
    #Create a stream reader to get the file's encoding and contents.
    $sr = New-Object System.IO.StreamReader($filePath, $true)
    [char[]] $buffer = new-object char[] 3
    $sr.Read($buffer, 0, 3)  
    $encoding = $sr.CurrentEncoding
    $sr.Close()
    
    #Show the detected encoding.
    $encoding
    
    #Update the file contents.
    $content = [IO.File]::ReadAllText($filePath, $encoding)
    $content2 = $content -replace "my" , "your"
    
    #Save the updated contents to file.
    [IO.File]::WriteAllText($filePath, $content2, $encoding)
    
    #Display the result.
    Get-Content $filePath
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.