CodexBloom - Programming Q&A Platform

PowerShell 7.3 - advanced patterns When Using Start-Process with Redirected Output in a Non-Interactive Session

👀 Views: 78 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
powershell start-process output-redirection

I've looked through the documentation and I'm still confused about I keep running into I'm sure I'm missing something obvious here, but I'm working with an scenario with PowerShell 7.3 when using `Start-Process` to execute a script in a non-interactive session. I want to capture the output of my script, but it seems that the output redirection is not functioning as expected. Here's the command I'm using: ```powershell Start-Process -FilePath "C:\Path\To\MyScript.ps1" -RedirectStandardOutput "C:\Path\To\Output.txt" -NoNewWindow -Wait ``` When I run this command, the output file is created, but it remains empty. I've confirmed that `MyScript.ps1` is executing and should produce output, as I can see it when I run the script directly in an interactive PowerShell session. To debug further, I added some logging within `MyScript.ps1`: ```powershell Write-Output "This is a test output" Write-Host "This should not go to the output file" ``` I expected "This is a test output" to appear in `Output.txt`, but it doesn't. However, when I run the script normally, I see both lines in the console. Additionally, I've tried using `-NoProfile` and `-ExecutionPolicy Bypass` in the `Start-Process` command to rule out profile issues and permission constraints, but that didn't resolve the question either. Is there something I'm missing with `Start-Process` in a non-interactive context? How can I ensure that the output from my script is redirected correctly to the file? This is part of a larger service I'm building. For reference, this is a production application. Is there a simpler solution I'm overlooking? This is my first time working with Powershell LTS. Any feedback is welcome!