Q: I'm trying to pass a bunch of objects from one command to another in PowerShell. The command complains that it doesn't take pipeline input. How can I pass my list of objects for further processing?

John Savill

March 15, 2011

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A: I was recently writing a script to return information on remote machines based on a list from Active Directory (AD). I wanted to pass my computer objects gained from an AD query into the get-wmiobject cmdlet. However, the -computer switch of get-wmiobject didn't accept a passed list of objects via the pipeline, so I got an error.

The solution is to use the ForEach-Object cmdlet instead to pass attributes from the piped list of objects that can be accessed using the $_.. For example, consider the command

 Get-ADComputer -filter \{lastLogonTimestamp-gt $lastLogon\} | get-wmiobject Win32_ComputerSystem

 This fails because the objects from Get-ADComputer can't be processed by get-wmiobject as a piped list of objects. However, the following works great:

 Get-ADComputer -filter \{lastLogonTimestamp -gt $lastLogon\} | ForEach-Object \{get-wmiobject Win32_ComputerSystem -computer $_.name\}

Notice that all I did was wrap the second command in the ForEach-Object and pass the $_.name for the -computer switch input.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like