1

I want to open an image in Windows Photo Viewer and then close it automatically using PowerShell. How would I achieve this?

I tried using the Start-Process cmdlet to open an image with the default application and have it return the associated process ID. The image is opened, but I don't get the process ID. This is probably because the Windows Photo Viewer-process is packed inside dllhost.exe.

PS C:\Users\Public\Pictures> $process = Start-Process -FilePath .\Wiki-error.jpg -PassThru Start-Process : This command cannot be run completely because the system cannot find all the information required. At line:1 char:12
+ $process = Start-Process -FilePath .\Wiki-error.jpg -PassThru + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand PS C:\Users\Public\Pictures> $process.ID
PS C:\Users\Public\Pictures> Stop-Process $process.ID
Stop-Process : Cannot bind argument to parameter 'Id' because it is null. At line:1 char:14 + Stop-Process $process.ID + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Stop-Process], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopProcess Command

I run PowerShell 2.1 and Windows 7. Any suggestions?

Magnus
  • 589
  • 8
  • 26
  • Windows Photo Viewer is a "difficult", concrete example I stumbled upon in my experiments. I am looking for a procedure which I can generalize to open and close an arbitrary file. – Magnus Aug 15 '17 at 09:06
  • 1
    I tried the following without any error: `PS> $proc = Start-Process .\image.jpg -PassThru; $proc.Id` and it returned `12576`. – Clijsters Aug 15 '17 at 10:14
  • @Clijsters This is weird. I can't reproduce it without error. Do you run with or without administrative privileges? What version of PowerShell are you running? I see that my machine has 2.0.1.1. – Magnus Aug 15 '17 at 10:45
  • 1
    Without. Oh. Wait. 5.1. You should really add that information to your question. And your operating system. And so on – Clijsters Aug 15 '17 at 10:46
  • I will. Even when trying on a system with PowerShell 5.1 and Windows 10, I get the same error. Screenshot: https://image.ibb.co/jGaGBa/powershell_start_process.png – Magnus Aug 15 '17 at 10:56
  • @Clijsters Which application does your image open in? Windows Photo Viewer or the one that's embedded in Windows 10? What is the file location of the process according to Task Manager? – Magnus Aug 15 '17 at 10:59
  • Yes, `rundll32.exe` – Clijsters Aug 15 '17 at 12:19

0 Answers0