Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Simple powershell script

Posted on 2011-03-04
8
797 Views
Last Modified: 2012-05-11
I have a script right now that looks for all files certain day old and certain file extension and it deletes all of the files. This works fine and it counts fine

Then I have to delete all folders that correspond to being empty and that includes all sub folders too. I also have to output this into a file and display each file deleted. The output would show 30 folders deleted but actually 48 were really deleted.

Now my question is i am trying to do a count of all the folders deleted. I have this script but it just counts the deepest folders not all the ones deleted. Here is the part of the script i can not get to count

$TargetFolder = "C:\Users\user\Desktop\temp"
$LogFile = "C:\Summary.txt"
$Count = 0

Date | Out-File -filepath $LogFile

get-childitem $TargetFolder -recurse -force | Where-Object {$_.psIsContainer}| sort fullName -des |
Where-Object {!(get-childitem $_.fullName -force)} | ForEach-Object{$Count++; $_.fullName} | remove-item -whatif | Out-File -filepath $LogFile -append

$Count = "Total Folders = " + $Count
$Count | Out-File -filepath $LogFile -append

0
Comment
Question by:pitster
  • 4
  • 4
8 Comments
 
LVL 5

Expert Comment

by:daveTechSearch
ID: 35039583
if you JUST run this.... is the count correct?
$TargetFolder = "C:\Users\user\Desktop\temp"

(
get-childitem $TargetFolder -recurse -force | Where-Object {$_.psIsContainer}| sort fullName -des |
Where-Object {!(get-childitem $_.fullName -force)}
).count

Open in new window

0
 
LVL 3

Author Comment

by:pitster
ID: 35055952
No the count shows 30.  So it is counting incorrectly but deleting the right amount.
0
 
LVL 3

Author Comment

by:pitster
ID: 35056154
It seems to just count the deepest folders and ignores the others.  This I do not understand i thought the recurse would count each folder?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 5

Expert Comment

by:daveTechSearch
ID: 35058848
Hmmm.... I just ran a similar test...

Nested folders with no files in them... and others with files.  The folders deleted were ONLY the deepest folder... I assume this would be due to parent folders *not* being empty... they contain other folders.  In my case, the count was also correct
0
 
LVL 5

Accepted Solution

by:
daveTechSearch earned 50 total points
ID: 35058880
0
 
LVL 3

Author Comment

by:pitster
ID: 35059776
not sure if you tested this at all but i get an error of

The term 'prune-directory' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is co
rrect and try again.
At line:1 char:16
+ prune-directory <<<<  c:\users\paul.healey\temp
    + CategoryInfo          : ObjectNotFound: (prune-directory:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
0
 
LVL 5

Expert Comment

by:daveTechSearch
ID: 35060894
prune-directory is actually a function that the author of the post wrote (see the article).

I am guessing that powershell is removing items from top level (rather than bottom up).. resulting in the skewed results... that is... all empty folders deleted, but the count does not add up for empty folders.

If I run this on my test folder:
$TargetFolder = "C:\folders\"

get-childitem $TargetFolder -recurse -force | ? {$_.psIsContainer} |
sort fullName -des | ? {!(get-childitem $_.fullName -force -recurse)} | select fullName

... the bottom level folders are listed.  If I replace the 'select' statement with the 'remove'... the top level folder is also removed.
0
 
LVL 3

Author Closing Comment

by:pitster
ID: 35130849
This got me to part of what i needed still having trouble getting a count but i can now at least display what I need
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question