Thursday, 18 February 2021

PowerShell: Check if the entered number is a valid Binary and print its decimal value

Powershell Script Description: 

Pass a number to a PowerShell script as an argument and validate whether it is a valid Binary. If the entered number is a valid binary, display its decimal value.


Script: isValidBinary.ps1

if (  $args[0] ){
    $num = $args[0]

    try {
        $value = [convert]::ToInt32(“$num”,2)
        Write-Host "'$num' is a valid Binary" -ForegroundColor Green
        Write-host "Decimal Value: $value"
        }

    catch{
        Write-Host  "'$num' is not a valid Binary" -ForegroundColor Red
    }
}
else{
    Write-Host "You must pass a number to the script"
}

Output:




 


No comments:

Post a Comment