SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

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

Tim Radney - Database Professional

Tim Radney - Database Professional by Tim Radney I am a Sr DBA for a top 40 US bank. I live in the south eastern US. I have been working with database since 1999 but only full time for the past three years.


No comments.

Leave a Comment

Please register or log in to leave a comment.