You can make Subject optional and handle the requirement at the beginning of the body of your script, simulating a mandatory parameter, as follows:
param(
[parameter(Mandatory=$true)][ValidateSet("Add","Delete")] [string]$Command,
[string] $Subject
)
if (($Command -eq 'Add') -and ($PSBoundParameters['Subject'] -eq $null)) {
$Subject = Read-Host 'Supply value for the parameter "Subject" (mandatory when the value of "Command" is "Add")'
}
The condition $PSBoundParameters['Subject'] -eq $null evaluates to True if the parameter Subject wasn't specified. Note that you can't simply use $Subject -eq $null, because $Subject is initialized as an empty string if the parameter is omitted. If you don't typecast $Subject (i.e., leave out the [string]), then it will be null if it's omitted, but I presume you don't want to do that.
Note that this will allow the user to simply hit [ENTER] when prompted, leaving $Subject blank, but that's standard behavior for mandatory parameters. If you don't want to allow that, you could do one of the following (this is another advantage of handling complex parameter requirements like this in the body rather than the parameter declaration).
Throw an error:
param(
[parameter(Mandatory=$true)][ValidateSet("Add","Delete")] [string]$Command,
[string] $Subject
)
if (($Command -eq 'Add') -and ($PSBoundParameters['Subject'] -eq $null)) {
$Subject = Read-Host 'Supply value for the parameter "Subject" (mandatory when the value of "Command" is "Add"'
if (-not $Subject) {
throw "The Subject may not be blank."
}
}
Keep prompting until a value is provided:
param(
[parameter(Mandatory=$true)][ValidateSet("Add","Delete")] [string]$Command,
[string] $Subject
)
if (($Command -eq 'Add') -and ($PSBoundParameters['Subject'] -eq $null)) {
do {
$Subject = Read-Host 'Supply value for the parameter "Subject" (mandatory when the value of "Command" is "Add"'
if (-not $Subject) {
Write-Host -NoNewline "The Subject may not be blank. "
}
} until ($Subject)
}