How do I open an image and then close it automatically in PowerShell?

2.3k views Asked by At

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?

0

There are 0 answers