Count of Files

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
JohnTitor
Posts: 107
Joined: Fri Nov 17, 2017 8:49 pm

Count of Files

Post by JohnTitor » Thu Mar 14, 2019 11:06 am

So I'm trying to show the count of about 40 different folders
Desktop
Documents
MozillaProfiles
Outlook
etc...

My current working theory is

$folder = "C:\users\user\desktop"
$DesktopCount = Get-ChildItem $folder -Force -Recurse -File | Measure-Object | %{$_.Count}

Then at the end of the script I do
$DesktopCount+$DocumentsCount+$DownloadsCount+$FavoritesCount+$LinksCount

Is there a better way of doing this?

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 11:53 am

It is not clear what you are tri ng to do.

$folders = 'desktop','documents', 'outlook' … etc

$count = (Get-Childitem $folders -File -Recurse).Count

User avatar
JohnTitor
Posts: 107
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 12:20 pm

I'm trying to provide the count of files of the folders I'm going to move through robocopy and display it to the technician
Then, once the robocopy job is done, I'm going to provide them the count on the new PC

The problem, is not all files are under the users profile so I can't just count that. They are spread throughout the PC so I have to do the individual counts.

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 12:42 pm

RoboCopy can count the files faster than PowerShell. Just select "log only" and extract the file count.

User avatar
JohnTitor
Posts: 107
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 12:48 pm

$folders = "C:\users\user\desktop","C:\users\user\documents"
$count = $null
foreach ($folder in $folders){

$rclog = robocopy $folder \xxx /L /E
$count += ($rclog -match 'new File' ).Count

}

$count

Looks like a winner!

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Count of Files

Post by jvierra » Thu Mar 14, 2019 1:05 pm

Little trick...

Create a new folder. Symlink target folders into new folder. Run RoboCopy from new folder root and all can be copied/counted/moved in one command.

User avatar
JohnTitor
Posts: 107
Joined: Fri Nov 17, 2017 8:49 pm

Re: Count of Files

Post by JohnTitor » Thu Mar 14, 2019 1:11 pm

Huh? lol

I'll give you some examples of the different folders I'm moving

"C:\users\$user\desktop"
"C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default"
"C:\Users\$user\AppData\Local\Microsoft\Feeds"
"C:\Users\$user\AppData\Local\Microsoft\Internet Explorer\EmieUserList"
"C:\Users\$user\AppData\Roaming\Mozilla\Firefox"
"C:\Users\$user\AppData\Roaming\Microsoft\Outlook"

Post Reply