/errors/powershell/cannot-convert-value-to-type
Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format."
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
- tool
- PowerShell
- exit code
- 1
- versions
- Windows PowerShell 5.1, PowerShell 7.x
- os
- windows, linux, macos
Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format." means a cast ([int]'abc'), a typed variable or a typed parameter received a value PowerShell cannot convert. The inner error explains why: for numbers the string is not numeric; for dates, String was not recognized as a valid DateTime. Check the value, then convert it safely with -as or an explicit parse format.
Reproduce it
$ErrorActionPreference = 'Stop'
foreach ($sb in { [int]'abc' }, { [datetime]'31/12/2026' }, { [int]$n = 5; $n = 'five' }, { Get-ChildItem -Depth 'two' }) {
try { & $sb } catch { $_.Exception.Message }
}Output
Cannot convert value "abc" to type "System.Int32". Error: "Input string was not in a correct format." Cannot convert value "31/12/2026" to type "System.DateTime". Error: "String was not recognized as a valid DateTime." Cannot convert value "five" to type "System.Int32". Error: "Input string was not in a correct format." Cannot bind parameter 'Depth'. Cannot convert value "two" to type "System.UInt32". Error: "Input string was not in a correct format."
Casts use the invariant culture, so [datetime]'31/12/2026' fails even on a machine whose regional format is day/month/year; [datetime]'2026-12-31' (ISO 8601) works everywhere.
Convert safely
'abc' -as [int]
"null from -as: $($null -eq ('abc' -as [int]))"
$n = 0
"TryParse('42'): $([int]::TryParse('42', [ref]$n)) -> $n"
[datetime]::ParseExact('31/12/2026', 'dd/MM/yyyy', [cultureinfo]::InvariantCulture).ToString('yyyy-MM-dd')
[int]' 42 '
[int]'1,024'.Replace(',', '')Output
null from -as: True
TryParse('42'): True -> 42
2026-12-31
42
1024| Need | Use |
|---|---|
| Number or $null, no exception | $value -as [int] |
| Test and convert | [int]::TryParse($s, [ref]$n) |
| Date in a known format | [datetime]::ParseExact($s, 'dd/MM/yyyy', [cultureinfo]::InvariantCulture) |
| Size strings like 10MB | PowerShell numeric literal 10MB, or parse the number and multiply |
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_conversion
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_type_operators
- learn.microsoft.comlearn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact
Related pages
errors · powershellPowerShell: Unable to find type [System.Windows.Forms.MessageBox].A [TypeName] literal names a .NET type that is not loaded. Load its assembly with Add-Type -AssemblyName, fix the namespace, or use using module for classes.errors · powershellPowerShell: You cannot call a method on a null-valued expression.You called a method on a value that is $null: a misspelled variable, a property that does not exist or a command that returned nothing. Check it first.flags · powershellConvertTo-Json -Depth: why nested objects become strings like System.Collections.HashtableConvertTo-Json serializes only 2 levels by default and turns deeper objects into strings. Pass -Depth 10 (maximum 100) to keep nested data as real JSON.