Use PowerShell to Find Metadata from Photograph Files
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to obtain metadata from photo files.
Hey, Scripting Guy! I need some help. I have thousands of photo files on my computer. I need to bring some order to them. I know that I can use Get-ChildItem from Windows PowerShell, and I can find the size of files and names of files. But I need to access information that is more specific to the photos, such as what camera was used, what the F-stop was, and the photo resolution. I know there are expensive software programs that might provide this information, but the information must come from somewhere, and maybe, just maybe, you can tell Windows PowerShell where that location is?
Microsoft Scripting Guy, Ed Wilson, is here. It is just a couple days until the Windows PowerShell Saturday event in Charlotte. If you have not yet registered, I think there are a couple of tickets left, so don’t delay too much or they will be gone. The list of speakers reads like a who’s who in the Windows PowerShell world. In fact, six of the speakers at our event in Charlotte are also going to be speaking at the Windows PowerShell Summit in Seattle in April. To register, go to PowerShell Saturday #007.
Note This is the fourth in a series of posts that talk about working with files and folders by using Windows PowerShell.
You should read the previous posts:
- List Files in Folders and Subfolders with PowerShell
- Use PowerShell to Create CSV File to Open in Excel
- List Music File Metadata in a CSV and Open in Excel with PowerShell
Commonly filled out metadata for image files contains the camera, the F-stop, resolution, and other useful information about the photo. This can be extremely useful for anyone who takes photos. The image metadata is easily found from the File tab for the photo by clicking Properties, then Details. The following image shows typical photo metadata:
When using the content view in File Explorer in Windows 8.1, a thumbnail appears with the dimensions of the photo, as shown here:
To use Windows PowerShell to examine this type of metadata means using the Shell.Application COM object, connecting to a file, and then walking through the metadata property bag. This technique is a bit cumbersome. Luckily, I can use the same function I wrote yesterday to accomplish this task.
Note The complete Get File Metadata function is available on the Script Center Repository.
Make sure to store results
Because it is possible that the script needs to iterate through thousands of photos, and for each photo it needs to search several hundred metadata attributes, the script will take some time to run. The best thing to do is to store the results in a variable. This will allow sorting and post collection processing of the data following the run.
The Get-FileMetaDataReturnObject.ps1 script contains a single function. The function is the Get-FileMetadata function. I load the function in the Windows PowerShell ISE, and run it to copy the function into memory. After I have done that, I call the function and pass it an array of folder paths. I get the array of folder paths by using the Get-ChildItem cmdlet. Here is the command that performs a recursive lookup of a folder named pics and pulls out the directory paths in that folder. This is a single line command that has wrapped.
$picMetadata = Get-FileMetaData -folder (Get-childitem E:\pics -Recurse -Directory).FullName
After the function completes running, I examine the contents of the $picMetaData variable to see what pieces of metadata are filled out. As shown in the following image, there is a decent amount of metadata supplied:
After perusing the list of metadata, I decide that I am interested in the camera model, dimensions of the photo, the f-stop, flash mode, iso speed, exposure time, focal length, size, and of course, the path to the photo. It is as easy as picking out chocolates from a candy box. Because I have stored the returned objects in the $picMetadata variable, I pipe it to the Select-Object cmdlet to produce the output. The command is shown here (this is a single-line command that has wrapped):
Select 'camera model', dimensions, f-stop, 'flash mode', 'iso speed', 'exposure time', 'focal length', size, path
The command and its associated output are shown in the following image:
Write it to a file
Now that I know that the command produces the output I desire, I pipe the results to the Export-CSV cmdlet as shown here (this is a single-line command):
Select 'camera model', dimensions, f-stop, 'flash mode', 'iso speed', 'exposure time', 'focal length', size, path |
Export-CSV -Path c:\fso\photoMetadata.csv -NoTypeInformation
Now, I use the Invoke-Item cmdlet to open the CSV file in Microsoft Excel. This command is shown here:
I can now use Microsoft Excel to examine and analyze the metadata associated with my photos. The following image illustrates this technique:
RR, that is all there is to using Windows PowerShell to obtain photo metadata. File Week will continue tomorrow when I will talk about finding neglected files.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy