---
title: "Cấu hình PowerShell tự động dọn file rác và nhật ký hệ thống trên Windows Server VPS"
description: "Hướng dẫn viết script PowerShell và cấu hình Task Scheduler tự động dọn file tạm, nén/xóa log IIS, Windows Update cũ trên Windows Server Cloud VPS."
canonical: "https://thuecloud.vn/cau-hinh-powershell-tu-dong-don-file-rac-va-nhat-ky-he-thong-tren-windows-server-vps"
markdown_url: "https://thuecloud.vn/cau-hinh-powershell-tu-dong-don-file-rac-va-nhat-ky-he-thong-tren-windows-server-vps.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:23.996Z"
updated_at: "2026-11-22T16:18:22.184Z"
site: "ThueCloud.Vn"
estimated_tokens: 2035
---
# Cấu hình PowerShell tự động dọn file rác và nhật ký hệ thống trên Windows Server VPS

> Hướng dẫn viết script PowerShell và cấu hình Task Scheduler tự động dọn file tạm, nén/xóa log IIS, Windows Update cũ trên Windows Server Cloud VPS.

- Canonical HTML: https://thuecloud.vn/cau-hinh-powershell-tu-dong-don-file-rac-va-nhat-ky-he-thong-tren-windows-server-vps
- Markdown URL: https://thuecloud.vn/cau-hinh-powershell-tu-dong-don-file-rac-va-nhat-ky-he-thong-tren-windows-server-vps.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Các thư mục nên và không nên dọn tự động trên Windows Server
- Script PowerShell dọn dẹp an toàn có ghi nhật ký
- Lên lịch chạy tự động bằng Task Scheduler
- Câu hỏi thường gặp (FAQ)
  - Chạy dism /online /Cleanup-Image /StartComponentCleanup có làm mất khả năng gỡ bản cập nhật Windows không?
  - Làm sao kiểm tra thư mục nào đang chiếm nhiều dung lượng nhất trên Windows VPS?
  - Có nên đặt lịch chạy script dọn rác mỗi giờ không?

Một trong những sự cố phổ biến nhất khi vận hành [Windows Server Cloud VPS](https://thuecloud.vn/vps-viet-nam) là phân vùng `C:\` bất ngờ đầy 100% sau vài tháng chạy ổn định, khiến SQL Server ngừng ghi dữ liệu và Remote Desktop báo lỗi không thể đăng nhập. Thủ phạm thường là hàng chục GB file log của IIS, thư mục `C:\Windows\Temp`, và bộ nhớ đệm Windows Update (`WinSxS`). Dùng **PowerShell** kết hợp **Task Scheduler** giúp bạn tự động hóa hoàn toàn việc dọn dẹp an toàn.

> **Tóm tắt nhanh (TL;DR):**
> - **4 vị trí ngốn dung lượng âm thầm:** Log IIS (`C:\inetpub\logs`), `C:\Windows\Temp`, User Temp và bộ nhớ tạm Windows Update.
> - **Quy tắc giữ log an toàn:** Chỉ xóa file log cũ hơn `14–30 ngày` để vẫn còn dữ liệu điều tra nếu có sự cố gần đây.
> - **Tự động chạy định kỳ:** Lên lịch qua Task Scheduler chạy mỗi tuần lúc 03:00 sáng Chủ nhật bằng quyền `SYSTEM`.
> - *Cập nhật: 11/2026*

## 1. Các thư mục nên và không nên dọn tự động trên Windows Server

| Đường dẫn thư mục | Có nên dọn tự động? | Thời gian giữ lại đề xuất | Ghi chú an toàn |
| :--- | :--- | :--- | :--- |
| `C:\inetpub\logs\LogFiles` | Có (rất nên) | 14–30 ngày | Tránh đầy ổ khi web có nhiều traffic |
| `C:\Windows\Temp` | Có | > 7 ngày | Bỏ qua các file đang bị tiến trình khóa |
| `C:\Windows\SoftwareDistribution\Download` | Có (khi Update xong) | > 14 ngày | Cần dừng nhẹ `wuauserv` nếu xóa toàn bộ |
| `C:\Windows\WinSxS` | Không xóa tay! | Dùng lệnh `DISM` | Xóa thủ công sẽ làm hỏng hệ điều hành |

## 2. Script PowerShell dọn dẹp an toàn có ghi nhật ký

Tạo file `C:\Scripts\Cleanup-Server.ps1` với nội dung sau. Tham số `-ErrorAction SilentlyContinue` đảm bảo script không bị dừng nếu gặp file đang được ứng dụng sử dụng:

```powershell
$LogFile = "C:\Scripts\cleanup.log"
$DaysToKeep = 14
$CutoffDate = (Get-Date).AddDays(-$DaysToKeep)

Add-Content -Path $LogFile -Value "=== Bắt đầu dọn dẹp: $(Get-Date) ==="

# 1. Dọn log IIS cũ hơn 14 ngày
if (Test-Path "C:\inetpub\logs\LogFiles") {
    Get-ChildItem -Path "C:\inetpub\logs\LogFiles" -Recurse -File |
        Where-Object { $_.LastWriteTime -lt $CutoffDate } |
        Remove-Item -Force -ErrorAction SilentlyContinue
}

# 2. Dọn Windows Temp cũ hơn 7 ngày
Get-ChildItem -Path "C:\Windows\Temp" -Recurse -File |
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } |
    Remove-Item -Force -ErrorAction SilentlyContinue

# 3. Dọn WinSxS component store an toàn bằng DISM
Start-Process -FilePath "dism.exe" -ArgumentList "/online /Cleanup-Image /StartComponentCleanup" -Wait -NoNewWindow

Add-Content -Path $LogFile -Value "=== Hoàn tất dọn dẹp: $(Get-Date) ==="
```

## 3. Lên lịch chạy tự động bằng Task Scheduler

Chạy lệnh PowerShell sau với quyền Administrator để đăng ký tác vụ chạy vào 03:00 sáng Chủ nhật hàng tuần:

```powershell
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\Cleanup-Server.ps1"
$Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am
Register-ScheduledTask -TaskName "AutoCleanupWindowsVPS" -Action $Action -Trigger $Trigger -User "SYSTEM" -RunLevel Highest
```

Bạn có thể kết hợp script này cùng các tác vụ quản trị khác như [triển khai Ollama + Open WebUI](https://thuecloud.vn/trien-khai-ai-automation-nganh-agency-marketing-seo-bang-ollama-open-webui-tren-cloud-vps), [tối ưu hệ thống LMS trực tuyến](https://thuecloud.vn/cau-hinh-nang-cao-giup-website-tang-chuyen-doi-tren-he-thong-khoa-hoc-truc-tuyen-learndash-tutorlms-voi-nginx-fastcgi-redis) hoặc [kinh nghiệm debug Playwright Trace Viewer](https://thuecloud.vn/playwright-trace-viewer-debug-kinh-nghiem-van-hanh-thuc-te-va-so-sanh-phuong-an-thay-the). Xem thêm [bảng giá Windows VPS](https://thuecloud.vn/pricing) và [FAQ vận hành](https://thuecloud.vn/faq).

> **Vận hành Windows Server ổn định trên ThueCloud.Vn:**
> Cụm **KVM Enterprise NVMe** tại `vn-hn` giúp thao tác đọc/xóa hàng trăm nghìn file log nhỏ và chạy DISM cực nhanh, cam kết **SLA 99,9%**, **VAT 0%** và **hoàn tiền 07 ngày**. Khởi tạo dễ dàng từ **VPS VN 2** (`78.000đ`–`90.000đ/tháng`) hoặc **VPS VN 3** (`139.000đ`–`170.000đ/tháng`).

## Câu hỏi thường gặp (FAQ)

### Chạy `dism /online /Cleanup-Image /StartComponentCleanup` có làm mất khả năng gỡ bản cập nhật Windows không?
Có thể xóa các phiên bản dự phòng của bản cập nhật cũ đã cài thành công, nhưng đổi lại giải phóng từ 3–8 GB ổ cứng C: một cách hoàn toàn hợp lệ do Microsoft khuyến nghị.

### Làm sao kiểm tra thư mục nào đang chiếm nhiều dung lượng nhất trên Windows VPS?
Bạn có thể dùng công cụ nhẹ như TreeSize Free hoặc chạy lệnh PowerShell kiểm tra kích thước các thư mục cấp 1 trong `C:\` trước khi viết script dọn.

### Có nên đặt lịch chạy script dọn rác mỗi giờ không?
Không cần thiết. Chạy mỗi tuần 1 lần vào khung giờ ít truy cập (02:00–04:00 sáng Chủ nhật) là tối ưu nhất để không ảnh hưởng đến I/O của phần mềm ban ngày.

## References / Sources

- https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item
- https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dism

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
