Clear-Content cmdlet in PowerShell deletes the content of the file without deleting the file. It deletes the text from the file, the file still exists but is empty.
While working on files-based operation, often we may need to clear the content of the file or delete the content of all files like log files in the specified directory. The PowerShell management module provides a Clear-Content cmdlet to clear the content of the file.
In this article, I will explain how to use the PowerShell Clear-Content cmdlet to remove content from text files and clear the content of all files in a specified directory.
Clear-Content Syntax
PowerShell Clear-Content
cmdlet deletes the content of the file. As a result, the file exists but the file is empty.
Clear-Content [-Path] <String[]> [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [-Stream <String>] [<CommonParameters>]
Clear-Content of File
To delete the content of a file in the specified directory, use Clear-Content
cmdlet in PowerShell as below
Clear-Content -Path .\PowerShell-File.txt
In the above clear content example, the Clear-Content cmdlet deletes the content of the text file specified by the Path parameter.
Delete Content of all files
Use the Clear-Content cmdlet in PowerShell to remove the content of all txt files in the given directory and subdirectories specified by a wildcard character.
Clear-Content "*\*.txt"
In the above PowerShell clear content example, the Clear-Content removes the content from a text file from the directory and subdirectories using the specified wildcard character ( *)
Clear Content of read-only file
Use the Clear-Content cmdlet in PowerShell to remove content from a read-only text file using the Force parameter.
If the file is read-only and if you try to use Clear-Content to remove content from the text file, it will throw an exception as below
PS D:\PowerShell> Clear-Content -Path .\DestinationFile.txt
Clear-Content : Access to the path 'D:\PowerShell\DestinationFile.txt' is denied.
At line:1 char:1
+ Clear-Content -Path .\DestinationFile.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (D:\PowerShell\DestinationFile.txt:String) [Clear-Content], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : ClearContentUnauthorizedAccessError,Microsoft.PowerShell.Commands.ClearContentCommand
It throws an error as “Clear-Content access to the path is denied, ClearContentUnauthorizedAccessError.
“
To avoid unauthorizedAccessError while deleting the text from the file, use the Force parameter below
Clear-Content -Path .\DestinationFile.txt -Force
In the above PowerShell example, the Clear-Content cmdlet clears the content of the read-only text file specified by Path and Force parameters.
Cool Tip: How to clear variable value in PowerShell!
Conclusion
I hope the above article on the Clear-Content cmdlet in PowerShell helps you to remove content from the text file and delete all files content in directory and subdirectories using a wildcard character.
Clear-Content in the PowerShell management module deletes the content of a file, it does not delete the file. You can read more about how to append content to a file using Add-Content cmdlet in PowerShell.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.