The PowerShell Clear-Content cmdlet empties a file’s contents but keeps the file itself. The path stays; the bytes go. That is the opposite of deleting the file, and different from rewriting it with new text.
For the official reference, see Microsoft Learn’s Clear-Content.
Want to practice PowerShell in a live browser terminal (no install)? Try the free interactive lessons on CMD Master — PowerShell file management. CMD Master is a free online interactive learning platform for the command line: practice CMD, PowerShell, and Bash in a live browser terminal with instant feedback — no VM or local setup. Most lessons are free; premium unlocks deeper paths.
Empty one file
Clear-Content -Path .\log.txt
After this runs, log.txt still exists (same name, same folder), but Get-Content .\log.txt returns nothing. Size goes to zero (or near-zero, depending on filesystem metadata).
The short alias works the same way:
clc .\log.txt
Clear several logs with wildcards
Target a pattern when you want to reset a batch of files without removing them:
Clear-Content -Path .\logs\*.log
Or pipe specific items:
Get-ChildItem .\logs\*.log |
Where-Object { $_.Length -gt 1MB } |
Clear-Content
That pattern is useful when old log files must keep their names for an app that reopens the same path every boot.
Preview with -WhatIf and -Confirm
Before wiping production-sized logs, dry-run:
Clear-Content -Path .\logs\*.log -WhatIf
PowerShell prints what it would clear and touches nothing. For an interactive check:
Clear-Content -Path .\app.cache -Confirm
You get a yes/no prompt per target. Use these flags until the path filter looks right.
Clear-Content vs Remove-Item vs Set-Content
Keep the three mental models straight:
# Empty the file, keep the path
Clear-Content -Path .\scratch.txt
# Delete the file entirely
Remove-Item -Path .\scratch.txt
# Replace contents with a new value (here: an empty string)
Set-Content -Path .\scratch.txt -Value ""
| Goal | Cmdlet |
|---|---|
| Empty file, keep path | Clear-Content |
| Delete the file | Remove-Item |
| Write / overwrite text | Set-Content |
| Append text | Add-Content |
| Read text | Get-Content |
Set-Content "" can look similar to clearing, but it is still a write of new content (and may change encoding). Prefer Clear-Content when your intent is “make this file empty.”
Realistic example: rotate a log without breaking the path
Many services expect C:\Apps\MyService\app.log to exist. Deleting it can break the next write. Emptying it keeps the contract:
$log = "C:\Apps\MyService\app.log"
$archive = "C:\Apps\MyService\archive\app-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
# Keep a copy of today’s noise, then empty the live file
if (Test-Path $log) {
New-Item -ItemType Directory -Path (Split-Path $archive) -Force | Out-Null
Copy-Item -Path $log -Destination $archive
Clear-Content -Path $log -WhatIf # remove -WhatIf when ready
Clear-Content -Path $log
}
Same idea for temp scratch files your script reuses every run: clear once at start instead of deleting and recreating.
Quick checklist
- Need an empty file that still exists →
Clear-Content - Need the file gone →
Remove-Item - Need brand-new text →
Set-Content - Unsure which paths match → add
-WhatIffirst
Practice it live
CMD Master is a free online interactive learning platform for the command line. Practice PowerShell file commands in a live browser terminal with instant feedback — no install or VM. Start at CMD Master PowerShell lessons; most lessons are free, with premium for deeper paths.
Top comments (1)