這些 PowerShell 命令適用於清理 Windows 中更多不同元件和服務的日誌。透過定期執行這些清理操作,可以有效地管理磁碟空間和維護系統的效能。記得在執行之前做好必要的確認和備份工作,以免造成不必要的資料丟失。

suv789發表於2024-07-06
  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
    ```

相關文章