这些 PowerShell 命令适用于清理 Windows 中更多不同组件和服务的日志。通过定期执行这些清理操作,可以有效地管理磁盘空间和维护系统的性能。记得在执行之前做好必要的确认和备份工作,以免造成不必要的数据丢失。

  1. 清理临时文件

    powershellCopy Code
    Remove-Item -Path "$env:TEMP\*" -Force -Recurse
    Remove-Item -Path "$env:LOCALAPPDATA\Temp\*" -Force -Recurse
  2. 清理回收站

    powershellCopy Code
    Clear-RecycleBin -Force
  3. 清理浏览器缓存(例如清理 Chrome 缓存):

    powershellCopy Code
    Remove-Item -Path "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\*" -Force -Recurse
  4. 清理 Windows 更新临时文件

    powershellCopy Code
    Get-WindowsUpdateLog | Remove-WindowsUpdateLog
  5. 清理 Windows 错误报告和日志文件

    powershellCopy Code
    Get-WinEvent -LogName "Application", "System" | Where-Object { $_.TimeCreated -lt (Get-Date).AddDays(-30) } | Remove-WinEvent
  6. 优化系统启动项: 可以通过 PowerShell 管理注册表或启动文件夹来禁用或管理启动项,这需要一些谨慎操作。

  7. 清理远程桌面连接历史记录

    powershellCopy Code
    Clear-History -CommandLineType "RemoteDesktop"
  8. 清理 Powershell 历史记录

    powershellCopy Code
    Clear-History
  9. 清理系统日志文件

    powershellCopy Code
    Clear-EventLog -LogName "Application", "System"
  10. 清理 IIS 日志文件

    powershellCopy Code
    Get-ChildItem -Path "C:\inetpub\logs\LogFiles" | Remove-Item -Force -Recurse
  11. 清理临时 IIS 文件

    powershellCopy Code
    Remove-Item -Path "C:\Windows\Temp\*" -Force -Recurse
  12. 清理 Windows 更新缓存

    powershellCopy Code
    Clean-CMUpdatePackage
  13. 清理 Windows Defender 定义文件缓存

    powershellCopy Code
    Clear-MpThreatCatalog
  14. 清理 Windows 更新日志和临时文件

    powershellCopy Code
    Remove-Item -Path "$env:LOCALAPPDATA\Temp\WindowsUpdateLogs\*" -Force -Recurse
  15. 清理 PowerShell 缓存

    powershellCopy Code
    Clear-Host
  16. 清理用户临时文件和配置

    powershellCopy Code
    Remove-Item -Path "$env:LOCALAPPDATA\Temp\*" -Force -Recurse
    Remove-Item -Path "$env:APPDATA\Microsoft\Windows\Recent\*" -Force -Recurse
  17. 清理 Windows 搜索索引

    powershellCopy Code
    Get-WindowsSearchIndex | Reset-WindowsSearchIndex
  18. 清理 Windows 更新备份文件

    powershellCopy Code
    Dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase
  1. 清理旧的 Windows 安装文件

    powershellCopy Code
    Dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase
  2. 清理 Windows Event 日志文件

    powershellCopy Code
    Clear-EventLog -LogName "Application", "System"
  3. 清理 Windows Update 下载的临时文件

    powershellCopy Code
    Remove-Item -Path "$env:SYSTEMROOT\SoftwareDistribution\Download\*" -Force -Recurse
  4. 清理 Windows 更新错误报告

    powershellCopy Code
    Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\Windows Error Reporting\*" -Force -Recurse
  5. 清理 PowerShell 临时文件

    powershellCopy Code
    Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\*" -Force -Recurse
  6. 清理 Windows 应用程序日志

    powershellCopy Code
    Get-WinEvent -LogName "Application" | Remove-WinEvent
  7. 清理 Windows 防火墙日志

    powershellCopy Code
    Get-NetFirewallProfile | Get-NetFirewallLog | Clear-NetFirewallLog
  8. 清理 Windows 系统日志

    powershellCopy Code
    Get-WinEvent -LogName "System" | Remove-WinEvent
  9. 清理 Windows 运行时组件日志

    powershellCopy Code
    Get-WinEvent -LogName "Runtime" | Remove-WinEvent
  10. 清理 Windows 应用程序故障诊断日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Diagnostics-Performance/Operational" | Remove-WinEvent
  11. 清理 IIS 日志(特定日期范围内):

    powershellCopy Code
    Get-ChildItem -Path "C:\inetpub\logs\LogFiles" -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force
  12. 清理 Windows PowerShell 工作流和任务注册表日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-PowerShell/Operational" | Remove-WinEvent
  13. 清理 Windows 安全审计日志

    powershellCopy Code
    Get-WinEvent -LogName "Security" | Remove-WinEvent
  14. 清理 Windows 组件事件日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-WinINet/Analytic" | Remove-WinEvent
  15. 清除临时文件

    powershellCopy Code
    Remove-Item -Path "$env:TEMP\*" -Force -Recurse
  16. 清除 Windows 更新缓存

    powershellCopy Code
    Remove-Item -Path "$env:SystemRoot\SoftwareDistribution\Download\*" -Force -Recurse
  17. 清除事件日志(需以管理员身份运行):

    powershellCopy Code
    wevtutil el | ForEach-Object {wevtutil cl $_}
  18. 清除用户临时文件

    powershellCopy Code
    Remove-Item -Path "$env:USERPROFILE\AppData\Local\Temp\*" -Force -Recurse
  19. 清除 Internet Explorer 缓存

    powershellCopy Code
    Clear-IECache
  20. 清除 Windows Store 缓存

    powershellCopy Code
    Get-AppxPackage -AllUsers | Where-Object {$_.Name -like "*WindowsStore*"} | Remove-AppxPackage
  21. 清除 PowerShell 缓存

    powershellCopy Code
    Clear-History
  22. 清除内存

    powershellCopy Code
    Clear-Host
  23. 优化系统磁盘

    powershellCopy Code
    Optimize-Volume -DriveLetter C -Defrag -Verbose
  24. 清理 Windows PowerShell Core 日志

    powershellCopy Code
    Get-WinEvent -LogName "Windows PowerShell Core/Operational" | Remove-WinEvent
  25. 清理 Windows Defender 防病毒日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Windows Defender/Operational" | Remove-WinEvent
  26. 清理 Hyper-V 虚拟机日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Hyper-V-Worker-Admin" | Remove-WinEvent
  27. 清理 Windows 搜索索引日志

    powershellCopy Code
    Get-WinEvent -LogName "Windows Search/Operational" | Remove-WinEvent
  28. 清理 DNS 服务器日志

    powershellCopy Code
    Get-WinEvent -LogName "DNS Server/Analytical" | Remove-WinEvent
  29. 清理 DHCP 服务器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DHCP Server Events/Admin" | Remove-WinEvent
  30. 清理 Windows 版本管理日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-WindowsUpdateClient/Operational" | Remove-WinEvent
  31. 清理 Windows 内核事件日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Kernel-EventTracing/Operational" | Remove-WinEvent
  32. 清理 Windows 远程桌面服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational" | Remove-WinEvent
  33. 清理 Windows 存储空间直通日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-StorageSpaces-Driver/Operational" | Remove-WinEvent
  34. 清理 Windows 进程活动日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-ProcessExit/Operational" | Remove-WinEvent
  35. 清理 Windows 进程启动日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-ProcessCreation/Operational" | Remove-WinEvent
  36. 清理 Windows 网络共享日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Shell-Core/Operational" | Remove-WinEvent
  37. 清理 Windows 升级日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-WindowsUpdateClient/Operational" | Remove-WinEvent
  38. 清理 Windows 存储传输服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-StorDiag/Operational" | Remove-WinEvent
  39. 清理 Windows 连接性解析器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-ConnectivityAnalyzer/Operational" | Remove-WinEvent
  40. 清理 Windows 设备管理器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DeviceManagement-Enterprise-Diagnostics-Provider/Admin" | Remove-WinEvent
  41. 清理 Windows 打印服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-PrintService/Operational" | Remove-WinEvent
  42. 清理 Windows 驱动程序框架服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DriverFrameworks-UserMode/Operational" | Remove-WinEvent
  43. 清理 Windows 存储日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Storage-ClassPnP/Operational" | Remove-WinEvent
  44. 清理 Windows 文件共享服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-SMBServer/Audit" | Remove-WinEvent
  45. 清理 Windows 安全审计日志

    powershellCopy Code
    Get-WinEvent -LogName "Security/AuditSuccess", "Security/AuditFailure" | Remove-WinEvent
  46. 清理 Windows 系统事件通知服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Sens/Operational" | Remove-WinEvent
  47. 清理 Windows 远程注册表服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-RemoteRegistry/Operational" | Remove-WinEvent
  48. 清理 Windows 电源管理日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Power-Troubleshooter/Operational" | Remove-WinEvent
  49. 清理 Windows DHCP 客户端日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DHCP-Client/Operational" | Remove-WinEvent
  50. 清理 Windows 安全中心日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-SecurityCenter/Operational" | Remove-WinEvent
  51. 清理 Windows 虚拟化日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Hyper-V-Worker-Admin/Operational" | Remove-WinEvent
  52. 清理 Windows 虚拟磁盘服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-VirtualDiskService/Operational" | Remove-WinEvent
  53. 清理 Windows 网络连接管理器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-NetworkConnectionBroker/Operational" | Remove-WinEvent
  54. 清理 Windows 安全日志

    powershellCopy Code
    Get-WinEvent -LogName "Security" | Remove-WinEvent
  55. 清理 Windows 自动更新日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-WindowsUpdateClient/Operational" | Remove-WinEvent
  56. 清理 Windows 远程桌面服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational" | Remove-WinEvent
  57. 清理 Windows 应用程序组件日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-AppModel-State/Operational" | Remove-WinEvent
  58. 清理 Windows 组件服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Com/Operational" | Remove-WinEvent
  59. 清理 Windows 启动日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Kernel-Boot/Operational" | Remove-WinEvent
  60. 清理 Windows 容器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Containers/Operational" | Remove-WinEvent
  61. 清理 Windows 定时任务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-TaskScheduler/Operational" | Remove-WinEvent
  62. 清理 Windows 设备健康状况日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DeviceHealth-Attestation/Operational" | Remove-WinEvent
  63. 清理 Windows 系统故障排除日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Diagnosis-DPS/Operational" | Remove-WinEvent
  64. 清理 Windows 文件共享服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-SMBServer/Operational" | Remove-WinEvent
  65. 清理 Windows 进程活动日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-ProcessCreation/Operational" | Remove-WinEvent
  66. 清理 Windows 任务栏通知区域日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Shell-Core/Operational" | Remove-WinEvent
  67. 清理 Windows 存储空间日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-StorageSpaces-Driver/Operational" | Remove-WinEvent
  68. 清理 Windows 证书服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-CertificateServices-Client-Lifecycle-System/Operational" | Remove-WinEvent
  69. 清理 Windows 移动设备管理日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Mobile-Broadband-Experience/Operational" | Remove-WinEvent
  70. 清理 Windows 系统恢复日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-SystemRestore/Operational" | Remove-WinEvent
  71. 清理 Windows 内核事件追踪日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Kernel-EventTracing/Operational" | Remove-WinEvent
  72. 清理 Windows 安全审计日志

    powershellCopy Code
    Get-WinEvent -LogName "Security" | Remove-WinEvent
  73. 清理 Windows 应用程序错误日志

    powershellCopy Code
    Get-WinEvent -LogName "Application" | Remove-WinEvent
  74. 清理 Windows 系统错误日志

    powershellCopy Code
    Get-WinEvent -LogName "System" | Remove-WinEvent
  75. 清理 Windows PowerShell 日志

    powershellCopy Code
    Get-WinEvent -LogName "Windows PowerShell" | Remove-WinEvent
  76. 清理 Windows DNS 审计日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DNS-Client/Operational" | Remove-WinEvent
  77. 清理 Windows DHCP 审计日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DHCP-Client/Operational" | Remove-WinEvent
  78. 清理 Windows 文件复制服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-DfsSvc/Operational" | Remove-WinEvent
  79. 清理 Windows 转发事件跟踪日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-EventForwarding/Operational" | Remove-WinEvent
  80. 清理 Windows 远程桌面服务日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-TerminalServices-RemoteConnectionManager/Operational" | Remove-WinEvent
  81. 清理 Windows 文件服务器资源管理器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-SRMSVC/Operational" | Remove-WinEvent
  82. 清理 Windows 存储池日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-StorageSpaces-ManagementAgent/Operational" | Remove-WinEvent
  83. 清理 Windows 扫描管理器日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-ScanManagement/Operational" | Remove-WinEvent
  84. 清理 Windows 分析引擎日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Resource-Exhaustion-Detector/Operational" | Remove-WinEvent
  85. 清理 Windows 网络连接日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-TCP/IP/Operational" | Remove-WinEvent
  86. 清理 Windows 网络驱动程序日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-NDIS/Operational" | Remove-WinEvent
  87. 清理 Windows 文件保护日志

    powershellCopy Code
    Get-WinEvent -LogName "Microsoft-Windows-Wininit/Operational" | Remove-WinEvent
    1. 清理 Windows 文件复制服务调试日志

      powershellCopy Code
      Get-WinEvent -LogName "Microsoft-Windows-DfsSvc/Debug" | Remove-WinEvent
    2. 清理 Windows 远程管理服务日志

      powershellCopy Code
      Get-WinEvent -LogName "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational" | Remove-WinEvent
    3. 清理 Windows 容器日志

      powershellCopy Code
      Get-WinEvent -LogName "Microsoft-Windows-Containers/Operational" | Remove-WinEvent
    4. 清理 Windows 集群日志

    ```powershell
    Get-WinEvent -LogName "Microsoft-Windows-FailoverClustering/Operational" | Remove-WinEvent
    ```
    1. 清理 Windows 数据保护日志
    ```powershell
    Get-WinEvent -LogName "Microsoft-Windows-DataProtection-Server/Operational" | Remove-WinEvent
    ```
    1. 清理 Windows 数据保护日志
    ```powershell
    Get-WinEvent -LogName "Microsoft-Windows-DataProtection-Client/Operational" | Remove-WinEvent
    ```
    1. 清理 Windows 数据库引擎服务日志
    ```powershell
    Get-WinEvent -LogName "Microsoft-Windows-SQLEngine/Operational" | Remove-WinEvent
    ```

 

posted @ 2024-07-06 23:31  suv789  阅读(8)  评论(0编辑  收藏  举报