PowerShell -PassThru Parameter
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.
-PassThru basics
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.
Basic -PassThru usage with Stop-Process
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.
Using -PassThru with Start-Process
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.
-PassThru with Wait-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.
Combining -PassThru with Where-Object
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.
-PassThru with Debug-Process
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.
Using -PassThru with Suspend-Process
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
.
-PassThru with 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.
Chaining multiple -PassThru operations
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.
Source
In this article, we have covered the -PassThru parameter in PowerShell.
Author
List all PowerShell tutorials.