Which is preferred in if statements

Hi,

So I’m new to this forum, and tried to search for my dilemma on this forum but could not find it right away. I do know my way a little around powershell and I was wondering what is the better practice?

Example A

if ($myVar -eq $null) { 
   #do something
else {
   #do something else
}

or Example B

if ($null -eq $myVar) { 
   #do something
else {
   #do something else
}

or Example C

if (!$myVar) { 
   #do something
else {
   #do something else
}

Which is the better approach and why?

Erik,
Welcome to the forum. :wave:t4:

hmmm … what did you search for? :wink: I get hundrets of thousands hits when I search for “powershell compare against $Null

Here are the two first hits I got:

For your example C - it depends to what the variable $myVar will be evaluated to. In PowerShell an empty variable or empty string or a 0 (zero) is considered to be $false. In your example you negate the result by adding an exclamation mark what’s an alias for the logical operator -not.

1 Like

Hi, welcome to the forum :wave:

Generally, example B when doing equality comparisons.

Example C isn’t quite the same, even though it might appear to be:

$myVar = $false

if (!$myVar) {
    Write-Host 'Hello'
}

if ($null -eq $myVar) {
    Write-Host "You won't see this."
}
1 Like

Hi Olaf,

Thanks for the reply. I didn’t search for comparision with $null, because I’m more interested in Example C, but forgot to emphasize my wishes :wink:. To elaborate a bit more I’ll provide a condensed/simplified code snippet I consider of using in my script:

$Manager = (Get-AdUser SamAccount -Properties *).Manager
if (!$Manager) {
   write-host "No manager, no email notification"
}
else {
   write-host "Manager found! Will send notification email!"
}

or

$Manager = (Get-AdUser SamAccount -Properties *).Manager
if ($null -eq $Manager) {
   write-host "No manager, no email notification"
}
else {
   write-host "Manager found! Will send notification email!"
}

So I am actually checking to see of an user has a manager in AD, and if so send the manager an email. To complete code how I accomplish this is currently irrelevant, it works, trust me :slight_smile: I just want to know which if statement is best practise. If you insist I can post the entire code snippet, just wanted to keep it simple here.

Some background info
I do know how operators (or its aliases) and such work, I have got my fair share of C++ and PowerShell experience. Also learned a lot from PowerShell for SysAdmins and currently going through Learn PowerScript in a month of lunches. And next in my lineup are The Pester Book and The PowerShell Scripting and Toolmaking Book. Just saying I don’t mind getting real technical.

Currently in the process of rewriting/optimizing some powershell scripts left by my predecessor, those were a complete mess. And while optimizing I run into these wonderings as posted above :sweat_smile:

To be honest - I don’t know if there is a best practice for this particular topic. I’d expect it depends on your personal preference or the rules applying to your company or team standards.

In general you may find some best practices for PowerShell in the

1 Like

I think best practice is to always evaluate based on the left side of the operator, which means $null -eq $something is ok and $something -eq $null basically is doing the evaluation on the wrong side. In the end it does not matter much, the end results will be the same.