last modified February 15, 2025
In this article, we will cover the -PassThru parameter in
PowerShell. This parameter returns objects that would otherwise not be
returned by default.
The -PassThru parameter tells cmdlets to return output objects
even when they normally wouldn't. Many cmdlets perform actions without
output by default. -PassThru forces these cmdlets to return
the affected objects. This is useful for chaining commands in pipelines.
By default, Stop-Process doesn't return any output when it
terminates processes. With -PassThru, it returns the stopped
process objects. This allows you to verify the action or pipe results.
Stop-Process -Name "notepad" -PassThru
This command stops all Notepad processes and returns their objects. Without
-PassThru, you would get no confirmation of the action.
Start-Process doesn't return the new process object by default.
With -PassThru, you get the Process object of the started
application. This is useful for tracking or managing the new process.
$proc = Start-Process notepad -PassThru $proc | Select-Object Id, Name, StartTime
This starts Notepad and stores its Process object in $proc. We then display selected properties of the new process.
Wait-Process normally doesn't return anything when waiting
completes. With -PassThru, it returns the waited-on process
object. This confirms which process was being monitored.
$proc = Start-Process notepad -PassThru Wait-Process -InputObject $proc -PassThru
This starts Notepad, waits for it to exit, then returns its process object. The output confirms which process was waited on.
You can filter processes before passing them to another cmdlet with
-PassThru. This example stops high-memory processes while
keeping the pipeline flowing. The filtered processes are passed through.
Get-Process | Where-Object { $_.WS -gt 100MB } |
Stop-Process -PassThru |
Format-Table Name, Id, WS -AutoSize
This finds processes using over 100MB of working set, stops them, and
formats the output. -PassThru enables the pipeline flow.
Debug-Process attaches a debugger without output by default.
With -PassThru, it returns the process being debugged. This
helps confirm the debugging target.
Debug-Process -Name "powershell" -PassThru
This attaches a debugger to PowerShell processes and returns them. Without
-PassThru, you wouldn't see which processes were affected.
Suspend-Process pauses processes without output by default.
With -PassThru, it returns the suspended process objects.
This verifies which processes were paused.
Suspend-Process -Name "notepad" -PassThru
This suspends Notepad processes and returns their objects. You can then
resume them later using Resume-Process.
Resume-Process resumes suspended processes without output.
With -PassThru, it returns the resumed process objects.
This confirms which processes were reactivated.
Resume-Process -Name "notepad" -PassThru
This resumes previously suspended Notepad processes and returns them. The output shows the processes that were brought back to running state.
You can chain multiple operations with -PassThru to maintain
the pipeline. This example starts, waits for, and stops a process while
keeping the object flowing through each step.
Start-Process notepad -PassThru | Wait-Process -PassThru | Stop-Process -PassThru
This starts Notepad, waits for it to exit, then stops it, with each cmdlet
passing the Process object to the next. -PassThru enables this
entire pipeline to work.
In this article, we have covered the -PassThru parameter in PowerShell.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PowerShell tutorials.