PowerTip: Use PowerShell to Count Images


Summary: Learn how to use Windows PowerShell to count images.

Hey, Scripting Guy! Question How can I use Windows PowerShell to count images on a drive?

Hey, Scripting Guy! Answer Use the Get-ChildItem cmdlet, specify the path, and send the results to the Measure-Object cmdlet,
           for example:

dir -Recurse -ea 0 -File -path e:\data -include "*.gif","*.jpg", "*.png", "*.bmp" | measure

Note  Dir is an alias for Get-ChildItem and Measure is an alias for Measure-Object.

Comments (4)

  1. Or, in a reusable function:

    Function Count-Files($Files)
    {
    $results = @()
    foreach($file in $files)
    {
    $results += New-Object PSObject -Property @{Name = $file; Count = (dir $file | measure | select -expandproperty count)}
    }
    return $results
    }

    Count-Files -Files @(‘*.jpg’,’*.bmp’,’*.png’,’*.gif’)

  2. lars says:

    It would be nice if You could include an output-example

  3. Ken Row says:

    What’s the "-ea 0"?

  4. ITGuy says:

    @KenRowe
    Ea is The -ErrorAction Switch, and the 0 is the same as Silently Continue.

Skip to main content