Blog Post

Get count of folders using Power Shell

,

More on my recent PowerShell project.  I recon I have taken on the challenge to handle this entire request using Posh.  It started out just moving folders based on the folder name that included the date in format MMDDYY.  Next was having to stop services before moving the folders.  After that came a request to email the number of folders that are being moved.

This gave me two challenges.  One was getting the count of the folders and second was how to email using Posh.  I started my search on getting the folder count, for one that was trouble enough because everything was related to getting file count.  After a few searches I found the Where {$_.PSIsContainer} syntax which helped greatly.

I found the ability to send email very simple using “Send-MailMessage” all you need is the syntax to include.

What ended up working for me was the following code.

.     $item = (Get-ChildItem “\\myserver\directory1\directory2\” | Where {$_.PSIsContainer})

.     $count = $item.count
.     Send-MailMessage -FROM “myemail@company.com” -To “users@company.com” -Subject “Folder Migration”  -body “Guess what, $count folders will be copied today.” -SmtpServer myemailrelay.company.com

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating