Temporary files can take up considerable space in a database and eventually slow down computers. Administrators can use the following PowerShell script to completely clear the contents of files remotely. If you want to delete the entire text file content using PowerShell, then we can use the Clear-Content command.
How to use clear-content in PowerShell to empty a single file
Execute the script to clear the complete contents of a file by providing the file path as a parameter. In the below example, the TXT file stored at C:\Users will be cleared. You can also directly use the Clear-Content command for the TXT file or other files to clear the entire content of the file.
Script
<#
.SYNOPSIS
This script can be used to clear contents of a file.
.DESCRIPTION
This script can be used to clear contents of a file.
.EXAMPLE
C:\PS> C:\Script\Clear_File.ps1 C:\Users\gautam-2374\Desktop test.txt
Clears the contents of the file.
#>
param([String] $path)
“File Content”
#Gets the content of a file.
Get-Content $path
#Clears the content of a file.
Clear-Content $path
“File Content Cleared!”
Get-Content $path