Posting additional information to complement @KeithHill's answer as an answer, as it is too long for a comment.
If an error record was created using the Write-Error cmdlet, it does not end and obeys the behavior specified by the -ErrorAction argument or the $ ErrorActionPreference system variable. Errors using throw terminate. See the documentation for Write-Error (note that PowerShell 4.0 and below do not have the -Exception parameter mentioned on the web page.) And about_Throw in the PowerShell help system.
If you want to add custom error information and make it the final error, throw from your catch block as follows:
catch { throw (New-Object System.Exception "Error connecting to server $($server).", $_.Exception) }
You can use Write-Error if you want the termination to proceed as indicated by the -ErrorAction argument. In PowerShell 4.0 and below, the Write-Error cmdlet does not allow the -Exception argument and therefore will not throw an InnerException. This means that the caller will need to examine the collection in the $ Error system variable if it needs to determine the original exception. In later versions you can use Write-Error -Message "Some additional error information." -Exception $_.Exception Write-Error -Message "Some additional error information." -Exception $_.Exception in its catch block.
source share