# $cs.Shell_ExecuteWithTimeout

> Executes a command and waits for it, like $cs.Shell_Execute, but forcibly kills the process if it hasn't exited within timeout seconds.

Source: https://docs.capaone.com/capainstaller/powerpacks/cs-shell-executewithtimeout/  
Product: CapaInstaller — a separate CapaSystems product; do not apply this page to any other.

### Description

Executes a command and waits for it, like [`$cs.Shell_Execute`](/capainstaller/powerpacks/cs-shell-execute/), but forcibly kills the process if it hasn't exited within `timeout` seconds. The window is always hidden. Use this instead of `$cs.Shell_Execute` when a command could hang and you need a hard upper bound on how long the PowerPack waits for it.

### Syntax

$cs.Shell\_ExecuteWithTimeout(string command, string arguments = "", bool mustexist = false, int timeout = 30)

### Parameters

#### command (String)

Command to execute.

#### arguments (String)

Arguments for the command. Default is empty.

#### mustexist (Boolean)

Check that `command` exists before executing; returns `5` without running anything if it doesn't. Default is `$false`.

#### timeout (Int32)

Seconds to wait before the process is forcibly killed. Default is `30`.

### Return value

Integer. The process's own exit code if it completes in time, or `0` if it had to be killed after the timeout expired.

### Example

**PowerShell**

```powershell
$MsiPath = Join-Path $global:Packageroot "kit" "GoogleChromeStandaloneEnterprise64.msi"
$Result = $cs.Shell_ExecuteWithTimeout("msiexec.exe", "/i `"$MsiPath`" /qn /norestart", $false, 120)
if ($Result -ne 0 -and $Result -ne 3010) {
  $cs.Job_WriteLog("Chrome installation failed or timed out with exit code: $Result")
  Exit-PSScript $Result
}
```

Running a vendor-supplied post-install script that has been known to hang, with `mustexist=$true` so a missing script doesn't silently pass and a short timeout so the PowerPack never gets stuck:

```powershell
$ScriptPath = Join-Path $global:Packageroot "kit" "PostConfig.cmd"
$Result = $cs.Shell_ExecuteWithTimeout($ScriptPath, "", $true, 60)
if ($Result -ne 0) {
  Exit-PSScript -exitcode 1 -exitmessage "PostConfig.cmd failed or was killed after timeout, exit code: $Result"
}
```

### Related functions

[$cs.Shell_Execute](/capainstaller/powerpacks/cs-shell-execute/)
