Skip to content

Instantly share code, notes, and snippets.

@arebee
Created November 17, 2015 02:29
Show Gist options
  • Select an option

  • Save arebee/6d105da5fdcfcc74160b to your computer and use it in GitHub Desktop.

Select an option

Save arebee/6d105da5fdcfcc74160b to your computer and use it in GitHub Desktop.
PowerShell script to save as UTF-8 without a BOM
gci . -recurse -filter *.ps* | % {
$MyFile = gc $_.Fullname -raw
$MyPath = $_.Fullname
[System.IO.File]::WriteAllLines($MyPath, $MyFile, [System.Text.UTF8Encoding]($False))
}
@arebee

arebee commented Nov 17, 2015

Copy link
Copy Markdown
Author

@me-suzy

me-suzy commented May 22, 2021

Copy link
Copy Markdown

hello. Where is the line of the root folder in which I can make the changes? Can you please write also this in the code?

@arebee

arebee commented May 22, 2021

Copy link
Copy Markdown
Author

hello. Where is the line of the root folder in which I can make the changes? Can you please write also this in the code?

I don't understand your question. This snippet acts in the current folder gci ..

@me-suzy

me-suzy commented May 22, 2021

Copy link
Copy Markdown

hello , sir. I have a lots of .txt files (not .py) located in *c:\Folder1* I need to run a Poweshell code as to convert all those files from UTF-8 to UTF-8-BOM. I am using Powershell from Windows10. Can this be done with the help of your code, if can be a little bit modify, of course.

@arebee

arebee commented May 22, 2021

Copy link
Copy Markdown
Author

Yes it can. Just modify the filter *.ps* to *.txt. Good luck!

@me-suzy

me-suzy commented May 25, 2021

Copy link
Copy Markdown

Yes, it is easy to replace the extension from .ps to .txt. But in your code, where is the PATH of the folder where should I make the changes? This is the problem. Sorry, I am not a developer, i know only to run PowerShell codes 💯

@fjh1997

fjh1997 commented Jan 21, 2024

Copy link
Copy Markdown

use $MyFile = gc -raw -Encoding utf8 $_.Fullname instead ,and note if you don't properly set encoding via-Encoding utf8 for Get-Content,some characters would be garbled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment