Clear Content - PowerShell


The Clear-Content cmdlet removes the entire content of the specified file without deleting the file. It is most commonly used on files that frequently need to have their content refreshed, such as log files or temporary data files, without the need to recreate the file itself.


Clear-Content [-Path] <String[]> [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]


  • -Path: Specifies the path to the file whose content you want to clear. Accepts absolute or relative paths. If not provided, the current working directory is used.
  • -Force: Forces clearing of the file’s content even if it is read-only. Use with caution.
  • -WhatIf: Shows what would happen if the command was run without actually modifying any files.
  • -Confirm: Prompts for confirmation before clearing the file’s content.


Simple Example: Clear the content of the file named “log.txt” in the current working directory:

Clear-Content log.txt

Complex Example: Forcefully clear the content of the read-only file “sensitive-data.txt” after confirmation:

Clear-Content -Path "sensitive-data.txt" -Force -Confirm

Common Issues

  • File Not Found: Ensure that the path to the file is correct and that the file exists.
  • Permission Denied: Verify that you have sufficient permissions to modify the file.


With Write-Output: Write new content to the cleared file:

Write-Output "New content" | Clear-Content -Path "new-file.txt"

With Get-ChildItem: Clear the content of all files in a directory:

Get-ChildItem -Path ".\logs\*.log" | Clear-Content
  • New-Item: Creates a new file or directory.
  • Remove-Item: Deletes a file or directory.
  • Set-Content: Sets the content of a file.