Piping Files and Folders

Let’s assume you want to create a function that accepts a file path. There are numerous tasks that can be done with files. You may want to copy files, archive them, turn them to hidden files, or whatever else comes to mind. We are not focused on what you do with files here. We want to look at how your PowerShell function can accept files from a user.

Basically, you now have two nested loops: process {} is the loop used by pipeline objects, and the foreach loop inside of it takes care of string arrays submitted by the user.

Now what if you wanted Get-ChildItem to provide paths to the function? It does not return strings. It returns filesystem objects, and inside of these, there is a property called “FullName” that has the path. This is what you’d do:

Now, the function would not only accept strings via pipeline (ValueFromPipeline), but also objects (ValueFromPipelineByPropertyName) that have a property named like the parameter (Path) or one of its aliases (FullName). Mission accomplished. Your function would now provide maximum flexibility for the user, and this is basically what cmdlets do too.

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.