Solved

PowerShell script to remove temp files and folders

Posted on 2008-10-09
8
2,361 Views
Last Modified: 2013-11-25
Hello everyone,

I'm very new to PS scripting, and i was hoping to get some help on creating a script that will do the following:

* access a remote computer
* parse through all users profiles (specifically %temp%/SAS Temporary folder) folder
* delete any files that are older than than 2 days
* email me a report with the amount of space freed due to the deletion of those temp files in that "SAS Temporary Folder" folder

please let me know if you need more details, and thanks in advance
0
Comment
Question by:bloodywicked
[X]
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
  • 4
  • 3
8 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22687513
OS?
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22687690
Here is one that should work against XP and 2003

If it works as you expect then remove the -whatif from remove-item
$tempfolders  = dir "\\$server\c$\documents and settings" -rec -force | ?{$_.PSIsContainer -and ($_.Name -eq "temp")}
foreach($folder in $tempfolders)
{
    $files = $folder.GetFiles() | ?{$_.LastWriteTime -lt (get-Date).adddays(-2)}
    foreach($file in $files)
    {
        $SavedSpace += $file.length
        $file.FullName | remove-Item -whatif -force
    }
}
"Total Space Saved: {0:n2}MB" -f ($SavedSpace/1mb)

Open in new window

0
 

Author Comment

by:bloodywicked
ID: 22689068
Hello BSonPosh,

thank you so much for you prompt respond.  the OS is windows XP.  However when i ran the script i got the following error.
PSerror.bmp
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:bloodywicked
ID: 22689107
i also have this script which i created with the help of an awesome book called "Powershell in Action", but i was hoping to get a loop in there instead of having to set two different Paths.

also, I'm not sure if this code will parse through the folders that are located with the %temp% folder for each user profile.

thank you

$path0 = "\\bai-sas1\c$\Documents and Settings\cwu\Local Settings\Temp\"
$path1 = "\\bai-sas1\c$\Documents and Settings\ehendarwan\Local Settings\Temp\"
 
 
$days = 2
 
get-ChildItem $path0,$path1 |Where-Object{$_.LastmodifiedTime -lt (get-Date).adddays(-$days)}

Open in new window

0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 500 total points
ID: 22689812
Interesting... it seems to think you have long path values.

Try this... just checking to see if it does anything
$tempfolders  = dir "\\$server\c$\documents and settings" -rec -force -ea 0 | ?{$_.PSIsContainer -and ($_.Name -eq "temp")}
foreach($folder in $tempfolders)
{
    $files = $folder.GetFiles() | ?{$_.LastWriteTime -lt (get-Date).adddays(-2)}
    foreach($file in $files)
    {
        $SavedSpace += $file.length
        $file.FullName | remove-Item -whatif -force
    }
}
"Total Space Saved: {0:n2}MB" -f ($SavedSpace/1mb)

Open in new window

0
 

Author Comment

by:bloodywicked
ID: 22691244
strange, this one seems to work fine, and it added the amount of space that would be freed.  can i add like a mailto: line that way i can get this information email to me ..

or do i need to echo that output into a txt file then email that
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22694121
You can get a send-smtpmail cmdlet from here http://www.codeplex.com/PowerShellCX/Release/ProjectReleases.aspx?ReleaseId=2688

Or you can use something like this



 
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpServer = "localhost"
$SmtpClient.host = 
$SmtpServer 
 
$From = "Friendly Reminder <User@example.com>"
$To = User2@example.com
$Title = "Subject Matter"
$Body = "Body Text" 
$SmtpClient.Send($from,$to,$title,$Body)  

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

734 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