How to Encode Without BOM in PowerShell

MD Aminul Islam Feb 02, 2024
  1. the UTF-8 Encoding System
  2. Encode a String Without BOM
  3. Encode a Text File Without BOM
How to Encode Without BOM in PowerShell

In this article, we’ll learn how to use UTF-8 encoding when creating or working with files without using the byte-order mark (BOM). In this article, we will use different example codes to learn two methods, working with string variables and files. But, first, let’s start with understanding the UTF-8 encoding system.

the UTF-8 Encoding System

Encoding with the UTF-8 is the default in the windows environment. Also, it’s a default in Windows PowerShell. The UTF-8, also known as UCS Transformation Format 8, is the most widely used and popular character encoding for any computer system.

In this encoding system, each character is represented by 1 to 4 bytes, and it is backward compatible with the ASCII and can represent any standard character. But if we don’t want to use UTF-8 when creating any file, we also can do that.

Now, we will see how to use UTF-8 encoding when creating or working with files without using BOM.

Encode a String Without BOM

Below is an example where we will encode a string without the use of BOM encoding. For example, see the code fence below.

Example Code:

$MyRawString = "It is a text."
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines("G:\PowerShell\out.txt", $MyRawString, $Utf8NoBomEncoding)

We first declare a string with a simple text in the above-shared example. After that, we disabled the BOM when working with the UTF-8 encoding by the line $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False.

Lastly, we rewrite all the lines and output them as text files. After executing the script shared above, we will get the below contents inside the output text file.

Output:

It is a text.

Encode a Text File Without BOM

Suppose we have the text file (sample.txt) with the following content:

It is the first line.
It is the second line.
It is the third line.
It is the fourth line.
It is the fifth line.

Below is an example where we will encode a text file without the use of BOM encoding. The code for our example will be as follows.

$MyRawString = Get-Content -Raw "G:\PowerShell\sample.txt"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines("G:\PowerShell\out.txt", $MyRawString, $Utf8NoBomEncoding)

First, we declared a variable that holds the data from the text file. Then, to extract the data from the text file, we used the built-in keyword of PowerShell, which is Get-Content.

After that, we disabled the BOM when working with the UTF-8 encoding by the line $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False.

Lastly, we rewrite all the lines and output them as text files. After executing the script shared above, we will get the below contents inside the output text file (out.txt).

Output:

It is the first line.
It is the second line.
It is the third line.
It is the fourth line.
It is the fifth line.

Please note that the example codes shared here are only executable on the Windows PowerShell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - PowerShell Encoding