开发者

Powershell script to delete temporary internet files

I'm trying to delete the files of extension ".htm" in the temporary internet files (TIF) folder using powershell. But couldn't get far on this.

Below is the script I have. I noticed that the cache name in the TIF folder is different from the display full name. (I tried to write a C# program to do this, but that doesn't work either)

get-childitem "c:\Documents and Settings\sanjeev-nithyanandam\Local Settings\Temporary Internet Files\*" -include *.htm -recurse -force | foreach ($_) {remove-item $_.fullname -force}

I learned from this post "Temporary Internet Files" folder on Windows that TIF folder behave differently than the other folders.

Is there a work around to modify the PS script and delete the files?开发者_如何学运维

Any help is appreciated.

Thanks! Sanjeev


I would not bother with the FullName at all:

get-childitem "c:\Documents and Settings\sanjeev-nithyanandam\Local Settings\Temporary Internet Files*" -include *.htm -recurse -force | remove-item -force -recurse


$a = Get-ChildItem C:\Scripts
foreach($x in $a)
{        
    $x.Delete()
}        


"ClearMyTracksByProcess"

Yes good if it did what it said on the box but if you take a peek at the super hidden folders MS has all over the place and Index.dat files then you would know it does not do all the name sugests.

shown below is code for removing most spyware files including FireFox and Flash cookies/shared objects and remember to kill process 'IExplore' before trying and you will also need to change the directory attributes.

Click my name if you want to know more.

using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics; 
using System.Text;

namespace Fidling
{
    public static class SpywareRemoval
    {
        private static void RemoveSpywareFiles(string RootPath, string Path,bool Recursive)
        {
            string FullPath = RootPath + Path + "\\";
            if (Directory.Exists(FullPath))
            {
                DirectoryInfo DInfo = new DirectoryInfo(FullPath);
                FileAttributes Attr = DInfo.Attributes;
                DInfo.Attributes = FileAttributes.Normal;
                foreach (string FileName in Directory.GetFiles(FullPath))
                {
                    RemoveSpywareFile(FileName);
                }
                if (Recursive)
                {
                    foreach (string DirName in Directory.GetDirectories(FullPath))
                    {
                        RemoveSpywareFiles("", DirName, true);
                        try { Directory.Delete(DirName); }catch { }
                    }
                }
                DInfo.Attributes = Attr;
            }
        }

        private static void RemoveSpywareFile(string FileName)
        {
            if (File.Exists(FileName))
            {
                try { File.Delete(FileName); }catch { }//Locked by something and you can forget trying to delete index.dat files this way
            }
        }

        private static void DeleteFireFoxFiles(string FireFoxPath)
        {
            RemoveSpywareFile(FireFoxPath + "cookies.sqlite");
            RemoveSpywareFile(FireFoxPath + "content-prefs.sqlite");
            RemoveSpywareFile(FireFoxPath + "downloads.sqlite");
            RemoveSpywareFile(FireFoxPath + "formhistory.sqlite");
            RemoveSpywareFile(FireFoxPath + "search.sqlite");
            RemoveSpywareFile(FireFoxPath + "signons.sqlite");
            RemoveSpywareFile(FireFoxPath + "search.json");
            RemoveSpywareFile(FireFoxPath + "permissions.sqlite");
        }

        public static void RunCleanup()
        {
            try { KillProcess("iexplore"); }
            catch { }//Need to stop incase they have locked the files we want to delete
            try { KillProcess("FireFox"); }
            catch { }//Need to stop incase they have locked the files we want to delete
            string RootPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToLower().Replace("documents", "");
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Macromedia\Flash Player\#SharedObjects",false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\#local", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Temporary Internet Files", false);//Not working
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.Cookies), true);
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), true);
            RemoveSpywareFiles("", Environment.GetFolderPath(Environment.SpecialFolder.History), true);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\Windows\Wer", true);  
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\Windows\Caches", false);          
            RemoveSpywareFiles(RootPath, @"AppData\Local\Microsoft\WebsiteCache", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Temp", false);
            RemoveSpywareFiles(RootPath, @"AppData\LocalLow\Microsoft\CryptnetUrlCache", false);
            RemoveSpywareFiles(RootPath, @"AppData\LocalLow\Apple Computer\QuickTime\downloads", false);
            RemoveSpywareFiles(RootPath, @"AppData\Local\Mozilla\Firefox\Profiles", false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Microsoft\Office\Recent", false);
            RemoveSpywareFiles(RootPath, @"AppData\Roaming\Adobe\Flash Player\AssetCache", false);
            if (Directory.Exists(RootPath + @"\AppData\Roaming\Mozilla\Firefox\Profiles"))
            {
                string FireFoxPath = RootPath + @"AppData\Roaming\Mozilla\Firefox\Profiles\";
                DeleteFireFoxFiles(FireFoxPath);
                foreach (string SubPath in Directory.GetDirectories(FireFoxPath))
                {
                    DeleteFireFoxFiles(SubPath + "\\");
                }
            }
        }

        private static void KillProcess(string ProcessName)
        {//We ned to kill Internet explorer and Firefox to stop them locking files
            ProcessName = ProcessName.ToLower();
            foreach (Process P in Process.GetProcesses())
            {
                if (P.ProcessName.ToLower().StartsWith(ProcessName))
                    P.Kill();
            }
        }
    }
}

Took me 2 days to delete index.dat files on windows 7 because MS keeps changing the rules so what worked on Win732 version XXX will not work on other versions that have updated service packs and the bottom line is you need to delete the files using another windows account that has admin rights, scripts at startup don't have admin rights by default.


You are probably better off getting windows to do it for you. This command should do the trick:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

Taken from how-to-geek.


$version = gwmi win32_operatingsystem | select version
$version = $version.version.substring(0,4)
$ErrorActionPreference = "Continue"
$process = Get-Process iexplore -ErrorAction silentlycontinue

if ($process) {
    "exists"
    Stop-Process $process -whatif

     }
else {
    "does not exist"

}

if ($version -ge "6.0."){
    [STRING]$ds = "C:\Users\"
}else{
    [STRING]$ds = "C:\Documents and Settings\"
}

sl $ds

foreach ($directory in get-childitem $ds -Force | where {$_.PsIsContainer}){
$dir =  $ds + $directory + "\Local Settings\Temporary Internet Files\Content.IE5"
get-childitem $dir -Force | where {$_.PsIsContainer} | Remove-Item -include *.htm -Force -Recurse
}


$WinTempInet = $env:windir + '\temp\Temporary Internet Files\Content.IE5'
if ($WinTempInet){
    sl $WinTempInet
    foreach ($WinTempInetDir in get-childitem $WinTempInet -Force | where {$_.PsIsContainer}){
    get-childitem $WinTempInetDir -Force | where {$_.PsIsContainer} | Remove-Item 
    -include *.htm -Force -Recurse`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜