Don't miss out on new posts! Sign up! Also, visit the VHT Forums!

Powershell if equals and not null

A while back, I wrote a blog post about using Powershell to email notify you if your home IP address changes if you are dynamically assigned an IP from your ISP.  However, I wanted to write a quick post about an update I have been meaning to do to tweak the script slightly to use just a touch more logic to keep from receiving false alarm emails from the process.


The powershell script works on a process that runs via scheduled task to hit a service to pull the current WAN ip.  Based on a text file set to the current known IP, a comparison is made and if there is a difference, you get an email which contains the current IP address.

The problem I started having with the script is that if the service to determine the public WAN ip didn’t return the value in an acceptable time period or something happened network wise to prevent the population of the IP text file with an IP address, the file would be modified and left empty.  As a result, emails would come through attaching the text file with the “new” IP address of “blank” even though the IP address had in fact not changed.

I needed some logic in the script to handle if the file was empty.  So the slight one liner change to allow powershell if equals and not null logic:

if (($ip -ne "") -And ($ip -ne $null)){

write-host "Mail Sent"


Again read the full post for the full script to email notify on IP address change, but adding the logic has allowed for proper alerting without the null empty email alerts if the service failed to obtain the proper public IP for some reason.


Look for a future write up of some other logic that I will be adding to the script.  Stay tuned!

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.