[int]$name = Read-Host Enter the KB number
If ($name -is [int]) {
wusa /uninstall /kb:$name
write-Host "This will uninstall windows update KB$name"
} else {
write-Host "Enter only the number"
}
Here in this PowerShell scripts, whenever a characters is typed is returns an error instead of message "Enter only the number".
PS C:\Users\User\Desktop> .\Test.ps1
45454
Enter the KB number: asfs
Cannot convert value "asfs" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\Users\User\Desktop\Test.ps1:5 char:1
+ [int]$name = Read-Host Enter the KB number
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger
This will uninstall windows update KB0
$erroractionpreference = "silentlycontinue"before definition, or define the variable as an int later.[int]$name = Read-Host Enter the KB number) in powershell console and enter a string. The same error will occur$nameis[int], however, on my first answer if you silently continue, the$?may not work as intended but both should work.