[powershell] How can prevent a PowerShell window from closing so I can see the error?

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:

& '\\net\DSFShare\Shared Data\Powershell Modules\Install-MyModuleManager.ps1'

It copies the script to the standard profile modules folder, then runs Install.ps1 from the module folder. Install.ps1 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?

The downloader script invokes the installer using:

$installerPath = [IO.Path]::Combine($LocalModulePath, 'Install.ps1')
Write-Host "Installer path: $installerPath"
if (Test-Path $installerPath) {
    Write-Host 'Install.ps1 exists.  Running Install.ps1'
    & $installerPath
}

Note, if from PowerShell, I populate $installerPath and call it using & $installerPath, I don't see the error.

I've checked the Application, System, Windows PowerShell and Security event logs and there aren't any errors relating to this.

All the script does is create an event source. If you want to run it, you can use:

Remove-EventLog -Source 'My.Module.Manager'

afterwards, to remove it. Here's the script:

Write-Host "Installing module..."
$eventSource = 'My.Module.Manager'

try {
    $sourceExists = [System.Diagnostics.EventLog]::SourceExists($eventSource)
} catch [Security.SecurityException] {
    Write-Verbose "Caught 'SecurityException': $_.Exception.Message" 
}

if ($sourceExists) {
    Write-Host "...installation complete..." 
} else {

    #region ----- Ensure-ProcessIsElevated -----

    if ($Verbose) {
        $VerbosePreference = "Continue"
    }
    if ($Debug) {
        $DebugPreference = "Continue"
    }

    Write-Debug "Command line is ___$($MyInvocation.Line)___"
    Write-Verbose "Entering script body"

    if ($ScriptPath) {
        Set-Location $ScriptPath
        Write-Verbose "Working directory: $pwd"
    }

    If (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
        Write-Warning "This script must be run with elevated privileges."
        Write-Warning "Restarting as an elevated process."
        Write-Warning "You will be prompted for authorization."
        Write-Warning "You may click 'No' and re-run manually, if you prefer."

        If ((Get-WmiObject Win32_OperatingSystem | select BuildNumber).BuildNumber -ge 6000) {
            Write-Verbose "This is a UAC-enabled system. Elevating ..."
            $CommandLine = "$($MyInvocation.Line.Replace($MyInvocation.InvocationName, $MyInvocation.MyCommand.Definition)) -ScriptPath $pwd"
            Write-Verbose "CommandLine: $CommandLine"

            Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList "$CommandLine"

        } else {
            Write-Verbose "The system does not support UAC: an elevated process cannot be started."
            Write-Warning "This script requires administrative privileges. Please re-run with administrative account."
        }

        Break
    }

    Write-Verbose "The script is now running with elevated privileges."

    #endregion ----- Ensure-ProcessIsElevated -----

    New-EventLog -LogName Application -Source $eventSource

    Write-Host "...installation complete..."
}

I'm using PowerShell 4.0 but there's no tag for it.

This question is related to powershell

The answer is


this will make the powershell window to wait until you press any key:

pause

Update One

Thanks to Stein. it is the Enter key not any key.


The simplest and easiest way is to execute your particular script with -NoExit param.

1.Open run box by pressing:

Win + R

2.Then type into input prompt:

PowerShell -NoExit "C:\folder\script.ps1"

and execute.


Also simple and easy:

Start-Sleep 10