Powershell脚本删除临时Internet文件

时间:2011-06-10 19:04:01

标签: powershell

我正在尝试使用PowerShell删除临时Internet文件(TIF)文件夹中的扩展名“.htm”。但是在这方面做得不够。

以下是我的脚本。我注意到TIF文件夹中的缓存名称与显示全名不同。 (我尝试编写一个C#程序来执行此操作,但这也不起作用)

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

我从这篇文章"Temporary Internet Files" folder on Windows中了解到,TIF文件夹的行为与其他文件夹不同。

是否有修改PS脚本并删除文件的工作?

感谢任何帮助。

谢谢! Sanjeev

5 个答案:

答案 0 :(得分:2)

我根本不打扰FullName:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

“ClearMyTracksByProcess”

如果它按照它在盒子上所说的那样做是好的,但是如果你看看MS遍布整个地方的超级隐藏文件夹和Index.dat文件,那么你会知道它没有完成所有名称的提取。< / p> 下面显示的是删除大多数间谍软件文件(包括FireFox和Flash Cookie /共享对象)的代码,并记得在尝试之前终止进程“IExplore”,您还需要更改目录属性。

如果您想了解更多信息,请点击我的名字。

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();
            }
        }
    }
}

我花了2天时间删除Windows 7上的index.dat文件,因为MS不断更改规则,因此在Win732版本XXX上工作的内容不适用于已更新Service Pack的其他版本,底线是您需要删除使用具有管理员权限的其他Windows帐户的文件,启动时的脚本默认没有管理员权限。

答案 3 :(得分:-1)

最好让Windows为你做这件事。这个命令可以解决这个问题:

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

取自how-to-geek

答案 4 :(得分:-1)

$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`