Q. Is there a way to pipe an object to multiple commands when using PowerShell?

A. Normally, when you use PowerShell you perform actions by piping the objects from one PowerShell command to another. For example:

get-aduser bruce | Disable-ADAccount

This command gets the object for user bruce and disables it using the Disable-ADAccount cmdlet. But what if you want to run another command on the bruce account as well? Say you want to pass the bruce object through the Disable-ADAccount cmdlet to the next cmdlet. Fortunately, this is easy. You just add -PassThru to the end of the cmdlet. For example, the command below would disable the AD account then update its description.

get-aduser bruce | Disable-ADAccount -PassThru
 | Set-ADUser -Description ("Account Disabled on
 $(get-date)")
Related Reading:

Check out hundreds more useful Q&As like this in John Savill's FAQ for Windows. Also, watch instructional videos made by John at ITTV.net.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering System Center 2012

During over 6 hours of training you can join John Savill from your computer as he will walk you through the key components and capabilities of System Center 2012, what’s involved in using the components, and the benefit they can bring to your environment.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.