I created this very simple PowerShell script:
using namespace System.IO
[CmdletBinding(SupportsShouldProcess)]
param
(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)][FileInfo[]]$Files
)
$Files.Length
$Files | Sort-Object | ForEach-Object {$_.Name}
When I call it with any result of a Get-ChildItem
call, $Files.Length
is always 1
, no matter how many files are in a directory:
PS C:\Temp> Get-ChildItem -File C:\Windows\ | .\Rename-Test.ps1
1
WMSysPr9.prx
What did I do wrong?
Basically your script is only missing a
process
block otherwise, the default block isend
thus you would be only seeing the last element coming from the pipeline. A minimal example of how you can approach it:As you may note, since the
-Files
parameter is typed[FileInfo[]]
(array ofFileInfo
instances), a loop is required in case the argument is passed via named parameter or positionally.If however you need to collect all input, i.e. for sorting it, then you would be needing a
List<T>
to collect each object in theprocess
block and then work with the collected input in theend
block, for example:Then for the above, both ways would work fine: