LINKS
In this PowerShell tutorial, I will explain how to convert byte array to string in PowerShell using various methods.
Using System.Text.Encoding
The most straightforward method to convert a byte array to a string in PowerShell is by using the System.Text.Encoding class. This class provides methods for encoding a set of characters into a sequence of bytes and for decoding a sequence of bytes into a set of characters. Here’s how you can use it:
In this example, we first use the GetBytes method of the UTF8 encoding to convert a string to a byte array. Then, we use the GetString method to convert the byte array back to a string. The UTF-8 encoding is commonly used because it supports all Unicode characters and is backward compatible with ASCII.
You can see the output in the screenshot below after I executed the script using VS Code.

Using Base64 Encoding
Another way to represent binary data as a string is by using Base64 encoding in PowerShell. This method is useful when you need to safely transmit binary data over a medium that only supports text. Here’s how you can convert a byte array to a Base64 encoded string:
To convert the Base64 string back to a byte array, you can use the FromBase64String method:
Using a StringBuilder and BitConverter
If you need to convert a byte array to a hexadecimal string representation, you can use the BitConverter class combined with a StringBuilder. Here’s an example:
The ToString method of the BitConverter class will return a string with the hexadecimal values separated by hyphens. If you want to remove the hyphens, you can use the Replace method:
# Remove hyphens from the hexadecimal string
$hexString = $hexString.Replace(“-“, “”)
# Display the cleaned-up hexadecimal string
$hexString
When converting byte arrays to strings, it’s crucial to understand the concept of encoding. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes. Decoding is the reverse process, converting a sequence of bytes back into a set of characters. The choice of encoding (e.g., UTF-8, ASCII, Unicode) affects how the bytes are interpreted and must be consistent during both encoding and decoding to avoid data corruption.
Conclusion
PowerShell provides flexible methods for converting byte arrays to strings, whether you need a plain text representation, a Base64 encoded string, or a hexadecimal string.
In this PowerShell tutorial, I have explained how to convert byte array to string in PowerShell.
You may also like:
There are different methods for getting string length in bytes in PowerShell. One popular method is to use the System.Text.Encoding method.
The size of a string in bytes depends on the text encoding used in PowerShell. Different encodings (like UTF-8, UTF-16, ASCII) represent characters differently, affecting the string’s byte size.
One direct approach to getting the byte size of a string in PowerShell is to use the System.Text.Encoding class. It will first convert the string to a byte array using a specific encoding and then measure the length of that array.
Here is an example.
In this example, the string “New York is the best city in the World!” is converted to a byte array using UTF-8 encoding, and the length of the byte array is then printed.
I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

Using the GetByteCount() Method
Let me show you another method to get the length of a string in PowerShell. You can use the GetByteCount() method in the System.Text.Encoding Class. This method directly returns the number of bytes required to encode a string without creating a byte array.
Here is a complete example.
I executed the above script, and you can see the output in the screenshot below:

Using Different Encodings
You can also use different encodings to see how the byte length varies. Here are examples using UTF-16 and ASCII encodings.
UTF-16 Example
Read Case Insensitive Strings Comparison in PowerShell
Handling Complex Strings
The byte length can vary significantly for strings containing special characters or non-English alphabets based on the encoding used.
Here is an example of how to get the length of a string with special characters in PowerShell.
You can see the output in the screenshot below after I executed the PowerShell script using VS code.

I explained how to get string length in bytes using various methods in this PowerShell tutorial. One of the best ways is to use the System.Text.Encoding class.
I have also explained how to get the length of a string with special characters or symbols in PowerShell.


