How to Get String Length in Bytes in PowerShell?

When working with strings in PowerShell, you might required to determine the size of a string in bytes. In this tutorial, I will share different methods with examples to get the string length in bytes using PowerShell.

To get the length of a string in bytes in PowerShell, you can use the System.Text.Encoding class. For example, using UTF-8 encoding, you can convert the string to a byte array with [System.Text.Encoding]::UTF8.GetBytes($string) and then get the length with $utf8Bytes.Length.

Get String Length in Bytes in PowerShell

There are different methods for getting string length in bytes in PowerShell. One popular method is to use the System.Text.Encoding method.

Method 1: Using System.Text.Encoding

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.

# Define the string
$string = "New York is the best city in the World!"

# Convert the string to a byte array using UTF-8 encoding
$utf8Bytes = [System.Text.Encoding]::UTF8.GetBytes($string)

# Get the length of the byte array
$byteLength = $utf8Bytes.Length

# Output the byte length
Write-Output "The string length in bytes (UTF-8): $byteLength"

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:

Get String Length in Bytes in PowerShell

Method 2: 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.

# Define the string
$string = "New York is the best city in the World!"

# Get the byte count using UTF-8 encoding
$byteCount = [System.Text.Encoding]::UTF8.GetByteCount($string)

# Output the byte count
Write-Output "The string length in bytes (UTF-8): $byteCount"

I executed the above script, and you can see the output in the screenshot below:

PowerShell Get String Length in Bytes

Method 3: 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

# Define the string
$string = "Hello, World!"

# Get the byte count using UTF-16 encoding
$byteCountUTF16 = [System.Text.Encoding]::Unicode.GetByteCount($string)

# Output the byte count
Write-Output "The string length in bytes (UTF-16): $byteCountUTF16"

ASCII Example

# Define the string
$string = "Hello, World!"

# Get the byte count using ASCII encoding
$byteCountASCII = [System.Text.Encoding]::ASCII.GetByteCount($string)

# Output the byte count
Write-Output "The string length in bytes (ASCII): $byteCountASCII"

Read Case Insensitive Strings Comparison in PowerShell

Method 4: 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.

# Define a string with special characters
$string = "Hello, 世界!"

# Get the byte count using UTF-8 encoding
$byteCountSpecial = [System.Text.Encoding]::UTF8.GetByteCount($string)

# Output the byte count
Write-Output "The string length in bytes (UTF-8, special characters): $byteCountSpecial"

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

How to Get String Length in Bytes in PowerShell

Conclusion

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.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.