diff --git a/setup.ps1 b/setup.ps1
index 3a60f7436328acbf410933cd341aa3cfbedfe962..61324cf4a7d64094f5ead498adf64719c3290f06 100644
--- a/setup.ps1
+++ b/setup.ps1
@@ -41,12 +41,12 @@ mkdir -Force build_cpp
 mkdir -Force $env:AIDGE_INSTALL_PATH
 Set-Location build_cpp
 cmake -DCMAKE_INSTALL_PREFIX:PATH=$env:AIDGE_INSTALL_PATH -DCMAKE_BUILD_TYPE=Debug ..
-if(!$?) { Set-Location $PSScriptRoot; Exit $LASTEXITCODE }
+if(!$?) { $lastError = $LASTEXITCODE; Set-Location $PSScriptRoot; Exit $lastError }
 cmake --build . -j2
-if(!$?) { Set-Location $PSScriptRoot; Exit $LASTEXITCODE }
+if(!$?) { $lastError = $LASTEXITCODE; Set-Location $PSScriptRoot; Exit $lastError }
 cmake --install . --config Debug
-if(!$?) { Set-Location $PSScriptRoot; Exit $LASTEXITCODE }
+if(!$?) { $lastError = $LASTEXITCODE; Set-Location $PSScriptRoot; Exit $lastError }
 # Optional: run the unit tests
 ctest --output-on-failure
-if(!$?) { Set-Location $PSScriptRoot; Exit $LASTEXITCODE }
+if(!$?) { $lastError = $LASTEXITCODE; Set-Location $PSScriptRoot; Exit $lastError }
 Set-Location $PSScriptRoot