18

How do I get PowerShell to wait until the Invoke-Item call has finished? I'm invoking a non-executable item, so I need to use Invoke-Item to open it.

arathorn
  • 2,058
  • 3
  • 20
  • 29

4 Answers4

25

Just use Start-Process -wait, for example Start-Process -wait c:\image.jpg. That should work in the same way as the one by @JaredPar.

Vladislav Rastrusny
  • 28,763
  • 23
  • 91
  • 155
stej
  • 27,607
  • 11
  • 68
  • 102
19

Pipe your command to Out-Null.

Shay Levy
  • 114,369
  • 30
  • 175
  • 198
8

Unfortunately you can't by using the Invoke-Item Commandlet directly. This command let has a void return type and no options that allow for a wait.

The best option available is to define your own function which wraps the Process API like so

function Invoke-Command() {
    param ( [string]$program = $(throw "Please specify a program" ),
            [string]$argumentString = "",
            [switch]$waitForExit )

    $psi = new-object "Diagnostics.ProcessStartInfo"
    $psi.FileName = $program 
    $psi.Arguments = $argumentString
    $proc = [Diagnostics.Process]::Start($psi)
    if ( $waitForExit ) {
        $proc.WaitForExit();
    }
}
JaredPar
  • 703,665
  • 143
  • 1,211
  • 1,438
2

One easy way

$session = New-PSSession -ComputerName "xxxxx" -Name "mySession"
$Job = Invoke-Command -Session $session -FilePath "xxxxx" -AsJob
Wait-Job -Job $Job
Dr Coyo
  • 51
  • 1
  • 4