Characters are stored internally as numeric codes—usually Unicode or ASCII values—and sometimes you need these numbers. For example, you might need to convert the letter 'A' into its numeric value 65 when preparing data for encryption or checking character ranges.
In this tutorial, I explain multiple methods for converting characters into integers in PowerShell with real examples.
Characters and Integers in PowerShell
In PowerShell, a char is a single Unicode character, while an int represents a whole number. Even a single character you type is often stored as a string until you explicitly cast it.
Here’s an example:
$char = 'A'
$char.GetType().NameOutput:
StringHere is the exact output in the screenshot below:

That’s because 'A' without explicit casting is treated as a string, even though it contains only one character. Behind the scenes, every character has a numeric code that identifies it; with proper conversion, you can retrieve that value.
Check out PowerShell Print Variable
Method 1: Casting with [int][char]
PowerShell allows type casting using the syntax [type]value. When converting a character to an integer, the most direct method is:
[int][char]'A'Output:
65How it works:
[char]'A'converts the string'A'to aSystem.Chartype.[int]then converts that character into its Unicode numeric code.
You can apply this to multiple characters:
$chars = 'A','B','C'
$chars | ForEach-Object { [int][char]$_ }Output:
65
66
67You can check the exact output in the screenshot below:

This method is concise, works for both ASCII and Unicode characters, and is the go-to for most scripts.
Check out Concatenate String and Integer in PowerShell
Method 2: Casting with [byte][char]
If your goal is to stick within the ASCII range (0–255), you can cast to byte instead:
[byte][char]'a'Output:
97This method is useful when working with ASCII-only data, such as simple English text or protocols that do not require Unicode support. Be cautious—casting extended Unicode characters to byte may not return meaningful results or could throw errors.
Check out PowerShell Round to Nearest Whole Number
Method 3: Using .ToCharArray() and Casting
When processing entire strings, .ToCharArray() is an excellent tool. It splits your string into individual characters, letting you process each one:
$string = "Hello"
foreach ($ch in $string.ToCharArray()) {
$code = [int][char]$ch
Write-Output "$ch = $code"
}Output:
H = 72
e = 101
l = 108
l = 108
o = 111Here is the exact output in the screenshot below:

This method is perfect for tasks like encoding text, analyzing strings, or building arrays of numeric codes for storage and processing.
Read How to Get the Type of an Object in PowerShell?
Method 4: Using [System.Text.Encoding]
PowerShell can access .NET classes for more complex encoding operations. One example is System.Text.Encoding, which returns byte arrays for characters based on specific encodings.
$char = 'A'
[System.Text.Encoding]::UTF8.GetBytes($char)Output:
65Method 5: Using [Convert]::ToInt32()
PowerShell also supports the .NET Convert class for type conversion. With it, you can make your intention clear to other developers:
[Convert]::ToInt32([char]'Z')Output:
90While not as compact as direct casting, this improves readability in scripts where clarity is more important than brevity.
Convert a String to ASCII Codes Example
Here’s a useful script that takes a string and converts each character to its ASCII code:
$text = "PowerShell"
$asciiCodes = $text.ToCharArray() | ForEach-Object { [int][char]$_ }
Write-Output $asciiCodesOutput:
80 111 119 101 114 83 104 101 108 108This can be applied to encryption, encoding, or even simple validation scripts where character codes are checked against a rule set.
Check out Concatenate Strings and Floats in PowerShell
Best Practices
- Use
[int][char]when you need a fast, clean conversion. - Use
.ToCharArray()for processing strings character-by-character. - Use
System.Text.Encodingwhen working within specific encoding formats. - Always test scripts with Unicode input if you expect multilingual data.
Common Mistakes to Avoid
- Forgetting to cast twice:
[int]'A' # Error
[int][char]'A' # Correct- Without the
charcast, PowerShell won’t know how to interpret'A'as an int. - Unicode vs ASCII confusion:
Using[byte][char]for non-ASCII characters may lead to data loss. Stick with[int][char]or encoding methods for Unicode data. - Not checking data types:
Always verify data types:
$val = [int][char]'A'
$val.GetType().NameConclusion
You’ve learned five different ways to achieve this, along with cases where each method excels. Whether you’re encoding data, processing text, or manipulating characters for logic, these techniques will make your scripts more powerful and flexible. In this tutorial, I explained how to convert a Char to an Int in PowerShell.
You may also like:
- Add an Element to the Beginning of an Array in PowerShell
- How to Set Variables in PowerShell?
- Generate Random Numbers in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.