Skip to content

Instantly share code, notes, and snippets.

@gpduck
Last active February 21, 2017 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpduck/aa31f708dbc75b4da694d251cfc0b936 to your computer and use it in GitHub Desktop.
Save gpduck/aa31f708dbc75b4da694d251cfc0b936 to your computer and use it in GitHub Desktop.

The whole point of using ValueFromPipeline (and ValueFromPipelineByPropertyName) is to replace this syntax:

"c:\windows","c:\Program Files" | ForEach-Object {
  Get-DirectoryFileSize -Directory $_
}

With this synax:

"c:\windows","c:\Program Files" | Get-DirectoryFileSize

Even when your function/cmdlet only supports operating on a single thing at a time, when you declare ValueFromPipeline/ValueFromPipelineByPropertyName on a property you need to include the process block so that a user can send a collection that PowerShell will unwrap for you and your function will still work.

Without a process block, the "Step 2" version of the function will only operate on the LAST item in the pipeline, which is not expected behavior.

"c:\windows","c:\program files" | Get-DirectoryFileSize
  
Directory         SizeInMB    
---------         --------
c:\program files         0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment