Powshell清理系统临时文件并记录清理日志
# 加载 WinForms 程序集 Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # 创建主窗口 $form = New-Object System.Windows.Forms.Form $form.Text = "达人iT清理工具" $form.Size = New-Object System.Drawing.Size(400, 200) $form.StartPosition = "CenterScreen" # 创建清理 Prefetch 按钮 $buttonPrefetch = New-Object System.Windows.Forms.Button $buttonPrefetch.Text = "清理 Prefetch 目录" $buttonPrefetch.Location = New-Object System.Drawing.Point(50, 50) $buttonPrefetch.Size = New-Object System.Drawing.Size(150, 30) # 创建清理 Temp 按钮 $buttonTemp = New-Object System.Windows.Forms.Button $buttonTemp.Text = "清理 Temp 目录" $buttonTemp.Location = New-Object System.Drawing.Point(200, 50) $buttonTemp.Size = New-Object System.Drawing.Size(150, 30) # 创建状态标签 $labelStatus = New-Object System.Windows.Forms.Label $labelStatus.Text = "状态:等待操作" $labelStatus.Location = New-Object System.Drawing.Point(50, 100) $labelStatus.Size = New-Object System.Drawing.Size(300, 20) # 将控件添加到窗口 $form.Controls.Add($buttonPrefetch) $form.Controls.Add($buttonTemp) $form.Controls.Add($labelStatus) # 清理 Prefetch 目录的函数 function Clean-Prefetch { $labelStatus.Text = "状态:正在清理 Prefetch 目录..." $form.Refresh() # 定义 Prefetch 目录路径 $prefetchPath = "C:\Windows\Prefetch" # 定义日志文件路径 $logFilePath = "$env:USERPROFILE\Desktop\Prefetch_Cleanup_Log.txt" # 获取 Prefetch 目录中的所有文件 $files = Get-ChildItem -Path $prefetchPath -File -ErrorAction SilentlyContinue # 初始化进度条 $totalFiles = $files.Count $currentFile = 0 # 创建日志文件并写入标题 "Prefetch 目录清理日志 - $(Get-Date)" | Out-File -FilePath $logFilePath "以下文件已被清理:" | Out-File -FilePath $logFilePath -Append "----------------------------------------" | Out-File -FilePath $logFilePath -Append # 遍历并删除每个文件 foreach ($file in $files) { $currentFile++ $percentComplete = ($currentFile / $totalFiles) * 100 Write-Progress -Activity "清理 Prefetch 目录" -Status "正在删除文件: $($file.Name)" -PercentComplete $percentComplete # 记录文件信息到日志 "文件名称: $($file.Name)" | Out-File -FilePath $logFilePath -Append "文件路径: $($file.FullName)" | Out-File -FilePath $logFilePath -Append "文件大小: $([math]::Round($file.Length / 1KB, 2)) KB" | Out-File -FilePath $logFilePath -Append "最后修改时间: $($file.LastWriteTime)" | Out-File -FilePath $logFilePath -Append "----------------------------------------" | Out-File -FilePath $logFilePath -Append # 删除文件 Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue } # 完成进度条 Write-Progress -Activity "清理 Prefetch 目录" -Status "完成" -Completed # 记录清理完成信息 "清理完成时间: $(Get-Date)" | Out-File -FilePath $logFilePath -Append "总共清理文件数量: $totalFiles" | Out-File -FilePath $logFilePath -Append $labelStatus.Text = "状态:Prefetch 目录清理完成!日志已保存到桌面。" $form.Refresh() } # 清理 Temp 目录的函数 function Clean-Temp { $labelStatus.Text = "状态:正在清理 Temp 目录..." $form.Refresh() # 定义 Temp 目录路径 $systemTempPath = "C:\Windows\Temp" $userTempPath = "$env:LOCALAPPDATA\Temp" # 定义日志文件路径 $logFilePath = "$env:USERPROFILE\Desktop\Temp_Cleanup_Log.txt" # 获取所有 Temp 目录中的文件 $systemTempFiles = Get-ChildItem -Path $systemTempPath -File -ErrorAction SilentlyContinue $userTempFiles = Get-ChildItem -Path $userTempPath -File -ErrorAction SilentlyContinue # 合并文件列表 $files = @($systemTempFiles) + @($userTempFiles) # 初始化进度条 $totalFiles = $files.Count $currentFile = 0 # 创建日志文件并写入标题 "Temp 目录清理日志 - $(Get-Date)" | Out-File -FilePath $logFilePath "以下文件已被清理:" | Out-File -FilePath $logFilePath -Append "----------------------------------------" | Out-File -FilePath $logFilePath -Append # 遍历并删除每个文件 foreach ($file in $files) { $currentFile++ $percentComplete = ($currentFile / $totalFiles) * 100 Write-Progress -Activity "清理 Temp 目录" -Status "正在删除文件: $($file.Name)" -PercentComplete $percentComplete # 记录文件信息到日志 "文件名称: $($file.Name)" | Out-File -FilePath $logFilePath -Append "文件路径: $($file.FullName)" | Out-File -FilePath $logFilePath -Append "文件大小: $([math]::Round($file.Length / 1KB, 2)) KB" | Out-File -FilePath $logFilePath -Append "最后修改时间: $($file.LastWriteTime)" | Out-File -FilePath $logFilePath -Append "----------------------------------------" | Out-File -FilePath $logFilePath -Append # 删除文件 Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue } # 完成进度条 Write-Progress -Activity "清理 Temp 目录" -Status "完成" -Completed # 记录清理完成信息 "清理完成时间: $(Get-Date)" | Out-File -FilePath $logFilePath -Append "总共清理文件数量: $totalFiles" | Out-File -FilePath $logFilePath -Append $labelStatus.Text = "状态:Temp 目录清理完成!日志已保存到桌面。" $form.Refresh() } # 绑定按钮点击事件 $buttonPrefetch.Add_Click({ Clean-Prefetch }) $buttonTemp.Add_Click({ Clean-Temp }) # 显示窗口 $form.ShowDialog()