advanced function process block problems

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Anthony Stringer Anthony Stringer 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #40310
    Profile photo of Peter Marsh
    Peter Marsh

    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
    + $hash=[ordered]@{
    + ~~~~~
    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.
    Profile photo of random commandline
    random commandline

    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 {
    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
        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
    Profile photo of Anthony Stringer
    Anthony Stringer

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.