Powershell invoke-command open jpeg minimised

Goce Dimitroski 41 Reputation points
2021-07-19T00:44:47.367+00:00

Hello,
I have written a PS script that opens and closes jpg in a directory via the invoke-command. (this is cause Azure File \Sync breaks images Thumbnail's )
But I want to do it so that it opens the jpg minimised

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
$folderForm = New-Object System.Windows.Forms.Form
$pathTextBox = New-Object System.Windows.Forms.TextBox
$selectButton = New-Object System.Windows.Forms.Button
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$openButton = New-Object System.Windows.Forms.Button
$okButton = New-Object System.Windows.Forms.Button
$cancelButton = New-Object System.Windows.Forms.Button
$finishButton = New-Object System.Windows.Forms.Button

<#
$formtext = New-Object System.Windows.Forms.Form
$formtext.Text = "Please Select the Directory that have the Pictures"
$formtext.Location = '20,45'
$formtext.KeyPreview = $True

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select the Directory"

$objForm.Size = New-Object System.Drawing.Size(300,200)

$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

>

$pathTextBox.Location = '23,23'
$pathTextBox.Size = '150,40'
$pathTextBox.ReadOnly = $true

$selectButton.Text = 'Select'
$selectButton.Location = '196,23'
$selectButton.Add_Click({
$folderBrowser.ShowDialog()
$pathTextBox.Text = $folderBrowser.SelectedPath
})

$openButton.Location = '40,90'
$openButton.text = 'Open Photo'
$openButton.Add_Click({

$PathsToSearch = @("C:\Temp")

$PathsToSearch = $folderBrowser.SelectedPath
$FailedItems = @()

$PathsToSearch | ForEach-Object {

$Path = $_
$JPGs = Get-ChildItem $Path | Where-Object { $_.Extension -eq ".jpg" -or $_.Extension -eq ".tif" } | Select-Object fullname -ExpandProperty fullname
$Counter = 0

Write-Host "Found $($JPGs.count) JPG files in ""$Path""" -ForegroundColor Green

$JPGs | ForEach-Object {
$Counter = $Counter + 1
$JPG = $_

#Open the picture

        Try{

            Invoke-Item $JPG -erroraction stop

        }
        Catch{
            #write-host "Failed to open ""$JPG"". Skipping...." -foregroundcolor red
            $FailedItems += $JPG
        }

    Start-Sleep -Seconds 1

    #close the picture
    try {
        <# Kill TimerEmail Process as script is being attended to #>

        $process = Get-Process Microsoft.Photos -ErrorAction SilentlyContinue 
        $process.kill()
      #  Write-Warning "`t""$JPG"" closed."
    }
    catch {
     }

}

}
Write-Host " PHOTOs are brought back . Please close the Application "

})

$cancelButton.Text = 'Cancel'
$cancelButton.Location = "56,215"
$finishButton.Text = 'Finish'
$finishButton.Location = "56,180"

$folderForm.CancelButton = $cancelButton
$folderForm.CancelButton = $finishButton

$folderForm.Text = 'Retrive Photo from Archive'
$folderForm.Controls.Add($pathTextBox)
$folderForm.Controls.Add($selectButton)
$folderForm.Controls.Add($openButton)
$folderForm.controls.Add($cancelButton)
$folderForm.controls.Add($finishButton)
$folderForm.ShowDialog()

END

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-07-19T01:48:10.08+00:00

    Using Invoke-Item is the same as double-clicking the JPG file, and you have no control over how that works.

    If you want to manage the window size you should be using Start-Process with the -WindowStyle parameter.

    0 comments No comments

  2. Goce Dimitroski 41 Reputation points
    2021-07-19T02:22:12.587+00:00

    I did try it with start-process $ JPG -windowstyle minimised
    but it still bring up the image.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.