2

In a PowerShell script I'm trying to filter the output of the exiftool(-k).exe command below, using Select-String.

I've tried numerous permutations, but none work, and I always see the unfiltered output. What do I need to do to filter the output of this command?

Start-Process -FilePath "C:\PowerShell\exiftool(-k).exe" -ArgumentList test.jpg |
  Select-String -pattern 'GPS' -SimpleMatch
mklement0
  • 382,024
  • 64
  • 607
  • 775
zBernie
  • 97
  • 2
  • 10
  • 1
    You say you want to filter the output, but you never explain what you're trying to filter out. If the command you posted isn't doing what you want, and you aren't sharing what you want in the text of the post, it is difficult for people to figure out what you want. Please elaborate. – Matthew Feb 19 '20 at 17:48
  • 1
    Can you share sample output of the tool? Might also be worth checking if the tool outputs to `stderr` (easy to verify with `Start-Process ... 2>$null`) – Mathias R. Jessen Feb 19 '20 at 17:49
  • I'm trying to filter out the string 'GPS' from the output. Here's the output of the exiftool command: ``` ExifTool Version Number : 11.84 File Name : test.jpg Directory : . White Balance : Auto Focal Length In 35mm Format : 26 mm Scene Capture Type : Standard Image Unique ID : C12QSJB01SB GPS Latitude Ref : North GPS Longitude Ref : West GPS Altitude Ref : Unknown (1.7) GPS Time Stamp : 23:43:26 ``` – zBernie Feb 19 '20 at 18:17
  • 1
    Good info, but hard to see line breaks in the comment. Maybe you can edit your original post and add the code block there? – Matthew Feb 19 '20 at 18:30

2 Answers2

1
  • You cannot directly receive output from a Start-Process call[1], so using it in a pipeline is pointless.

    • In fact, on Windows your program launched with Start-Process runs in a different, new window, which is where you saw the unfiltered output (given that no Select-String was applied there); in your calling window, Start-Process produced no output at all, and therefore nothing was sent to Select-String, and the pipeline as a whole produced no output.
  • Never use Start-Process to synchronously invoke a console application whose output you want to capture or redirect - simply call the application directly:

& "C:\PowerShell\exiftool(-k).exe" test.jpg | Select-String GPS -SimpleMatch

Note that &, the call operator, is needed for this invocation, because your executable path is (double-)quoted (of necessity here, because the file name contains ( and )); & is only needed for executable paths that are quoted and/or contain variable references; you wouldn't need it to call git ..., for instance.


[1] While you would see the program's output in the caller's window if you added -NoNewWindow -Wait to a Start-Process call, you still wouldn't be able to capture, pass on or redirect it.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

The naming is inconvenient. Rename it to exiftool.exe and run it without start-process.

rename-item 'exiftool(-k).exe' exiftool.exe    

C:\Users\bfbarton\PowerShell\exiftool.exe test-jpg | Select-String GPS 

Or

$env:path += ';C:\Users\bfbarton\PowerShell'
exiftool test.jpg | Select-String GPS

The website recommends to 'rename to "exiftool.exe" for command-line use'. https://exiftool.org . Even in unix, it wouldn't work without escaping the parentheses.

There's also the option of using the call operator. Using tab completion actually does this:

& '.\exiftool(-k).exe' test.jpg | select-string gps
js2010
  • 23,033
  • 6
  • 64
  • 66
  • That did nothing, i.e., no output. That was with and without the (-k). "C:\Users\bfbarton\PowerShell\exiftool(-k).exe" | Select-String -Pattern 'GPS' – zBernie Feb 19 '20 at 18:30
  • I tried it with exiftool, exiftool(-k), exiftool(-k).exe, and tried enclosing the command in quotes, but I keep getting this error: + CategoryInfo : ObjectNotFound: (-k:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – zBernie Feb 19 '20 at 19:09
  • Thanks for your help that worked! I just removed the (-k) and renamed the executable to exiftools.exe. Good enough! – zBernie Feb 19 '20 at 19:49
  • Maybe good to know: "The ExifTool application for Windows has the ability to take command-line arguments from inside the brackets in its own name." https://exiftool.org/forum/index.php?topic=12050.msg65104#msg65104 – PeterCo Aug 25 '22 at 14:30