PowerShell: check if WIA (COM?) object has a specific Property

312 views Asked by At

I'm still relatively new to PowerShell. This is a follow up to Use PowerShell to extract GPS Latitude etc. (Properties > Details) from an image file and using code like this (from @stackprotector):

# Create an ImageFile object and load an image file
$image = New-Object -ComObject Wia.ImageFile
$image.LoadFile("C:\Absolute\path\to\my.jpg")

# Read your desirered metadata
$image.Properties.Item('GpsLatitude').Value

As some images have no GPS data, this gets an error. How can I check if Properties.Item('GpsLatitude') actually exists in that file, before trying to extract it?

Thank you.

1

There are 1 answers

0
stackprotector On

For this specific purpose, you can use the Properties.Exists method:

if ($image.Properties.Exists('GpsLatitude')) {
    $image.Properties.Item('GpsLatitude').Value
}

In general, you can always use exception handling.