How to count files in a directory and its sub-directories

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5962

    by Giz059 at 2012-11-21 05:29:59

    Hi everybody,

    I'm quite new in the world of Powershell and i've been asked to get an information that seems easy to get in powershell but not so when you are in front of your screen 😉

    I would like to enumerate all the files contained in all the c:\windows\winsxs subdirectories and for each file i would like to have the number of times it appears

    For example : The file "win32k.sys" appears "21" times in all winsxs subdirectories.

    Thank's a lot for the expert advices !

    Ghislain

    by Klaas at 2012-11-21 06:04:26

    Like this?

    Get-Childitem -Recurse C:\Windows\Winsxs | Group-Object name | Select-Object count, name

    by selko at 2012-11-21 06:34:04

    Get-ChildItem -Path "C:\Windows\Winsxs" -Recurse | Where-Object { -not $_.PSIsContainer } | Group-Object Name | Select-Object Count,Name

    with this you only get the filenames without directories

    by Giz059 at 2012-11-21 06:39:21

    Yes perfect, i just add a date notion to have the file only modify within the past 3 months. It give this :

    $date= (get-date).AddMonths(-3)
    Get-ChildItem . *.sys -Recurse -force | Where-Object {$_.LastWriteTime -gt $date} | Group-Object name | Select-Object Count,LastWriteTime,Name,DirectoryName
    Get-ChildItem . *.inf -Recurse -force | Where-Object {$_.LastWriteTime -gt $date} | Group-Object name | Select-Object Count,LastWriteTime,Name,DirectoryName
    Get-ChildItem . *.exe -Recurse -force | Where-Object {$_.LastWriteTime -gt $date} | Group-Object name | Select-Object Count,LastWriteTime,Name,DirectoryName
    Get-ChildItem . *.dll -Recurse -force | Where-Object {$_.LastWriteTime -gt $date} | Group-Object name | Select-Object Count,LastWriteTime,Name,DirectoryName

    Can i include all the extension in the same line ? I do this because i don't want the directories but just the files.

    Thank's a lot

    by Giz059 at 2012-11-21 06:40:41

    thank's selko, you give me the answer for the directories before i finish my post 😉

    by Giz059 at 2012-11-21 06:46:05

    sorry, just a small problem, i cannot put both Where-Object {$_.LastWriteTime -gt $date} and Where-Object {-not $_.PSIsContainer}.
    It send me an error

    by selko at 2012-11-21 06:53:43

    you can do this so:
    Get-ChildItem . -Include *.sys,*.inf -Recurse -force | Where-Object {$_.LastWriteTime -gt $date -and -not $_.PSIsContainer } | Group-Object name | Select-Object Count,LastWriteTime,Name,DirectoryName

    by Klaas at 2012-11-21 07:06:35

    You can't select Lastwritetime or directoryname after you've grouped on name. Selected properties must either be the grouping properties or an aggregation.
    If the win32k.sys exists 21 times, what do you expect to be in the lastwritetime column? There may be 21 different lastwritetimes.

    by Giz059 at 2012-11-22 00:41:31

    ok i see. Thank's Klaas, i will delete the lastwritetime column, it's no use !

You must be logged in to reply to this topic.