Is it possible to force PowerShell script to throw if a required parameter is omitted?

Once the parameter is marked as mandatory PowerShell will prompt for value. That said, if you remove the mandatory attribute then you can set a default value with a throw statement:

function Deploy
{
    param(
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$BuildName=$(throw "BuildName is mandatory, please provide a value.")
    )

    Write-Host "Build name is: $BuildName"
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)