Wait for exit

I have the folling script that runs a batch file on a remote computer

$command = "c:\hello.bat"
$process = [WMICLASS]"\\$MachineName\ROOT\CIMV2:win32_process"  
$result = $process.Create($command)

How can I pause or wait to continue until this has completed?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

x-menIT super heroCommented:
assuming the process name created on the target machine is "cmd":

$command = "c:\hello.bat"
$process = [WMICLASS]"\\$MachineName\ROOT\CIMV2:win32_process"  
$result = $process.Create($command)
do {Start-Sleep -s 1} While ((Get-Process -Name cmd -computername $MachineName | Measure-Object).count -gt 0)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jfgrayAuthor Commented:
This seems to work but returns

Cannot find a process with the name "cmd". Verify the process name and call the cmdlet again.
At :line:50 char:41
+ do {Start-Sleep -s 1} While ((Get-Process <<<<  -Name cmd -computername $MachineName | Measure-Object).count -gt 0)

Is this expected ?
x-menIT super heroCommented:
yes, if after the process ends, the get-process will error because it is no longer running.

To suppress the error message, change the $ErrorActionPreference to “silentlycontinue”

$ErrorActionPreference = “silentlycontinue”

jfgrayAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.