If always reports failure

foreach($U in (Get-LocalUser -name test*|Where-Object{$_.Enabled -eq 'true'}))
{
    if(Disable-LocalUser -name $u.name -ErrorAction stop){ "Success"
    Write-Output "User $($u.name) disabled" |out-file C:\Windows\Temp\Disable.log -Append}
     else{"Failure"
      Write-Output "User $($u.name) could not be disabled" |out-file C:\Windows\Temp\Disable.log -Append}
    }type or paste code here

Hi there,
i am trying to do this basic error correction/logging just in case the command failes to disable any accounts it finds for whatever reason.
the command itself works fine but always reports “Failure” by the “if” statement and i think its something about the output of that command which is interperted as “not true”
any help/feedback is welcome(i already made it work with try/catch but more interested on why it didnt work with “if”)
Thanks

I’d recommend using proper error handling with a try catch block

already did that and it works
im more interested in knowing why this didn’t work :slight_smile: as i used this method before without issues

Since PowerShell considers a 0 (zero) or $null as false and everything else as true your if statement will be false whenever the command you have in your condition check does not return anything.

if ($null) {
    "Success"
}
else {
    "Failure"
}

returns

Failure

while

if ('something') {
    "Success"
}
else {
    "Failure"
}

returns

Success

Thanks a lot
now it make sense