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.
Asked
Active
Viewed 4.9k times
4 Answers
26
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
- 29,378
- 23
- 95
- 156

stej
- 28,745
- 11
- 71
- 104
19
Pipe your command to Out-Null.

Shay Levy
- 121,444
- 32
- 184
- 206
-
2Can you provide an example? `Invoke-Expression ... | Out-Null`? – The Muffin Man Jul 30 '14 at 16:39
-
@TheMuffinMan ` myprogram.exe | Out-Null ` http://stackoverflow.com/a/7908022/217593 – Michael Sync Oct 18 '16 at 01:38
9
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
- 733,204
- 149
- 1,241
- 1,454
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
- 61
- 1
- 4