Pipeline Input in PowerShell
Function Remove-IfEmpty {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)] $dir
)
process {
if (
(Test-Path $dir.fullname -PathType container) -and
((ls $dir.fullname -rec -file | measure | select -expand count) -eq 0)
) {
rm $_.fullname -confirm
}
}
}
ls -rec -dir | Remove-IfEmpty
ls -dir | select -first 1 | Remove-IfEmpty
Citing this,
ValueFromPipeline
: binds the parameter to the incoming object if the object is the same type as the parameter, or if it can be coerced to the same typeValueFromPipelineByPropertyName
: check the incoming object for aName
property. If it exists, bind theName
parameter to theName
property of the incoming object