- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to delete all the file contents using PowerShell?
If you want to delete the entire text file content using PowerShell, then we can use the Clear-Content command. For example, we have the below text file called Locations.txt on the C:\Temp path. You can check the content using the below file.
To clear the file content, we can use the below command.
Clear-Content C:\Temp\locations.txt -Force
-Force switch is used to clear the contents without user confirmation.
When you use this command with the pipeline in the Get-Content command, it will generate an IO Exception error that the file is in use because we are already retrieving the contents using Get-Content and then trying to delete the existing content.
Clear-Content : The process cannot access the file 'C:\Temp\locations.txt' because it is being used by another process. At line:1 char:37 + Get-Content C:\Temp\locations.txt | Clear-Content + ~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\Temp\locations.txt:String) [Clear-Content], IOException + FullyQualifiedErrorId : ClearContentIOError,Microsoft.PowerShell.Commands.ClearContentCommand
To clear the file contents for the CSV, we can first import that CSV file and then use the Clear() method to clear the file content. For example,
PS C:\> $csvfile = Import-Csv C:\Temp\Services.csv PS C:\> $csvfile.Clear()
In the above example, the CSV file stored at C:\temp will be cleared. You can also directly use the Clear-Content command for the CSV file or other files to clear the entire content of the file.
- How to delete the specific node from the XML file using PowerShell?
- How to delete azure blob (file) from the Storage account using PowerShell?
- How to delete the azure blob (File) using Azure CLI in PowerShell?
- How to delete all the tags of the azure resource using PowerShell?
- How to delete all users from specific OU using PowerShell?
- How to list the subfolder contents using Get-ChildItem using PowerShell?
- How to delete the windows certificate using PowerShell?
- How to delete registry Key using PowerShell?
- How to delete the Azure Resource Group using PowerShell?
- How to search in the file using PowerShell?
- How to edit the CSV file using PowerShell?
- How to mount the ISO file using PowerShell?
- How to dismount the ISO file using PowerShell?
- How to install the MSI file using PowerShell?
- How to get the file extension using PowerShell?