Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

Luke

share|improve this question

1 Answer 1

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 at 12:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.