title: How can prevent a PowerShell window from closing so I can see the error?
body: <p>I'm creating a local PowerShell module downloader script.  The module and the script are held on a network share.  The script is invoke using:</p>
  <pre><code>&amp; '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'
  </code></pre>
  <p>It copies the script to the standard profile modules folder and then runs Install.ps1 from the module folder.  <code>Install.ps1</code> elevates itself, if needed.  Just before the elevated window closes, a red error pops up, but the window closes too quickly for me to see the error.  How can I find out what the error is?</p>
  <p>The downloader script invokes the installer using:</p>
  <pre><code>$installerPath = [IO.Path]::Combine($LocalModulePath, 'Install.ps1')
  Write-Host &quot;Installer path: $installerPath&quot;
  if (Test-Path $installerPath) {
      Write-Host 'Install.ps1 exists.  Running Install.ps1'
      &amp; $installerPath
  }
  </code></pre>
  <p>Note, if from PowerShell, I populate <code>$installerPath</code> and call it using <code>&amp; $installerPath</code>, I don't see the error.</p>
  <p>I've checked the Application, System, Windows PowerShell, and Security event logs. There aren't any errors relating to this.</p>
  <p>All the script does is create an event source.  If you want to run it, you can use:</p>
  <pre><code>Remove-EventLog -Source 'My.Module.Manager'
  </code></pre>
  <p>afterwards, to remove it.  Here's the script:</p>
  <pre><code>Write-Host &quot;Installing module...&quot;
  $eventSource = 'My.Module.Manager'
  
  try {
      $sourceExists = [System.Diagnostics.EventLog]::SourceExists($eventSource)
  } catch [Security.SecurityException] {
      Write-Verbose &quot;Caught 'SecurityException': $_.Exception.Message&quot;
  }
  
  if ($sourceExists) {
      Write-Host &quot;...installation complete...&quot;
  } else {
  
      #region ----- Ensure-ProcessIsElevated -----
  
      if ($Verbose) {
          $VerbosePreference = &quot;Continue&quot;
      }
      if ($Debug) {
          $DebugPreference = &quot;Continue&quot;
      }
  
      Write-Debug &quot;Command line is ___$($MyInvocation.Line)___&quot;
      Write-Verbose &quot;Entering script body&quot;
  
      if ($ScriptPath) {
          Set-Location $ScriptPath
          Write-Verbose &quot;Working directory: $pwd&quot;
      }
  
      If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] &quot;Administrator&quot;)) {
          Write-Warning &quot;This script must be run with elevated privileges.&quot;
          Write-Warning &quot;Restarting as an elevated process.&quot;
          Write-Warning &quot;You will be prompted for authorization.&quot;
          Write-Warning &quot;You may click 'No' and re-run manually, if you prefer.&quot;
  
          If ((Get-WmiObject Win32_OperatingSystem | select BuildNumber).BuildNumber -ge 6000) {
              Write-Verbose &quot;This is a UAC-enabled system. Elevating ...&quot;
              $CommandLine = &quot;$($MyInvocation.Line.Replace($MyInvocation.InvocationName, $MyInvocation.MyCommand.Definition)) -ScriptPath $pwd&quot;
              Write-Verbose &quot;CommandLine: $CommandLine&quot;
  
              Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList &quot;$CommandLine&quot;
  
          } else {
              Write-Verbose &quot;The system does not support UAC: an elevated process cannot be started.&quot;
              Write-Warning &quot;This script requires administrative privileges. Please re-run with administrative account.&quot;
          }
  
          Break
      }
  
      Write-Verbose &quot;The script is now running with elevated privileges.&quot;
  
      #endregion ----- Ensure-ProcessIsElevated -----
  
      New-EventLog -LogName Application -Source $eventSource
  
      Write-Host &quot;...installation complete...&quot;
  }
  </code></pre>
  <p>I'm using PowerShell 4.0.</p>