r/PowerShell • u/SL-Tech • May 16 '26
Question Why doesn't my script close the active window?
When I run the script, the window doesn't close, even though exit is the last line. What am I missing? Thanks!
Script:
$profiles = Get-ChildItem -Path . -Recurse -Filter *.pubxml | Where-Object { $_.FullName -match "PublishProfiles" }
if (-not $profiles) {
Write-Host "No publish profiles found."
exit 1
}
foreach ($profile in $profiles) {
$project = Get-ChildItem -Path $profile.Directory.Parent.Parent.FullName -Filter *.csproj | Select-Object -First 1
if (-not $project) {
Write-Warning "No project file found for profile: $($profile.FullName)"
continue
}
$profileName = [System.IO.Path]::GetFileNameWithoutExtension($profile.Name)
Write-Host "Publishing $($project.Name) using profile $profileName ..."
dotnet publish $project.FullName -c Release /p:PublishProfile=$profileName
}
exit 1$profiles = Get-ChildItem -Path . -Recurse -Filter *.pubxml | Where-Object { $_.FullName -match "PublishProfiles" }
if (-not $profiles) {
Write-Host "No publish profiles found."
exit 1
}
foreach ($profile in $profiles) {
$project = Get-ChildItem -Path $profile.Directory.Parent.Parent.FullName -Filter *.csproj | Select-Object -First 1
if (-not $project) {
Write-Warning "No project file found for profile: $($profile.FullName)"
continue
}
$profileName = [System.IO.Path]::GetFileNameWithoutExtension($profile.Name)
Write-Host "Publishing $($project.Name) using profile $profileName ..."
dotnet publish $project.FullName -c Release /p:PublishProfile=$profileName
}
exit 1
5
Upvotes
7
u/justaguyonthebus May 16 '26
How are you running the script? Because all that does is exit the script. Has nothing to do with the window.
2
u/Zozorak May 16 '26
At a glance I can see
which looks to be a mistake and could be hanging. Therefore never hitting your exit at the end.
Try running in vscode for testing.
4
u/BrettStah May 16 '26
try this instead of Exit
[System.Environment]::Exit(0)
1
u/ron3090 May 16 '26
Are you pasting these commands into a PowerShell terminal, or is this in a ps1 file?
2
u/g3n3 May 16 '26
You’d need environment exit I believe.
1
u/g3n3 May 16 '26
Typically there isn’t a good use of exit ever. SQL agent I’ve used environment exit to push that exit code correctly. Just depends on what is the goal.
1
u/Icy-State5549 May 16 '26
Try exit;exit at the end.