Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In PowerShell you can change the name of the variable used as the item in an iterative block, such as:

dir -recurse *.snippet -OutVariable snippets

$snippets | Where-Object -PipelineVariable fn { $fn.Length -ge 6000 } | select FullName

That code doesn't work, but you get the idea. I think it doesn't work because the -PipelineVariable argument must be specified in the previous CmdLet.

My question is, what CmdLet do I use to pipe the contents of $snippets?

i.e. when I just run $snippets it implicitly just spews the contents out into the pipeline, but what CmdLet does this?

I've tried many, and many seem to output the array on the screen, but when piped into the Where-Object the where clause doesn't work, it's like its looking at the outer array object instance and not each item.


share|improve this question
up vote 2 down vote accepted

I would say it only makes sense to use -PipelineVariable if you have more than one step in the pipeline, to preserve object from earlier pipeline elements.

Anyway: in this case I would use Write-Output:

Write-Output $snippets -PipelineVariable fn | Where-Object { $fn.Length -ge 6000 }
share|improve this answer
Write-Output "Sends the specified objects to the next command in the pipeline" -- easy when you know. Nearest I got was Out-Default. Thanks and I take your point about PipelineVariable it was more a learning exercise than any legit need. – Luke Puplett May 27 '14 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .