I am trying to write an advanced function that will grab all files from an archive directory, list them, and total their size so I can see when I’m about to fill up a DVD. Below is a primary piece of script. When I insert this in the process script block by itself, does this mean every object produced by the get-childitem cmdlet is put thru a pipeline? Or just one big object? Is there even a pipeline involved at this point?
If I try to pipe this command to a select-object cmdlet, I can see the specified properties I’ve selected. However, if I then try to pipe to a hash table I get an error message stating:
At line:5 char:12
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (
, ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Get-ChildItem -Path C:\Users\Peter\Documents\Test_Archive -Filter '*.*' -Recurse -file -attributes archive
Any advice? Really getting frustrated.
Peter
This will display the size (in MegaBytes) of your archive and number of files. If you use the ‘-Export’ switch, it will export a list of files to each directory. You can use Robocopy as an alternative to Get-ChildItem for better performance.
function Get-ArchiveSize {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)]
[string[]]$Path,
[switch]$Export
)
Process {
foreach ($string in $path){
Write-Verbose "Searching [$string]"
$files = Get-ChildItem -Path $string -File -Attributes Archive -Recurse
$measure = $files | Measure-Object -Sum Length |
Select-Object @{n='Sum';exp={"{0:N2} (MB)" -f ($_.Sum / 1MB)}},
Count | Format-Table -AutoSize
$measure
If ($Export){$files.FullName | Out-File "$string\filelist.txt" -Append}
}
}
}
# Examples:
# Get-ArchiveSize -Path "\\path\one","\\path\two" -Verbose
# "\\path\one","\\path\two" | Get-ArchiveSize -Verbose -Export
You can use Robocopy as an alternative to Get-ChildItem for better performance.
now that i know how to use it, i do enjoy utilizing robocopy when necessary, however, i have not seen many detailed comparisons of performance vs get-childitem. could you please share how you determined this? i would like to use whichever is fastest, of course.