PowerShell Clear-Content

The Clear-Content cmdlet deletes the content of an item, but it does not delete the item, such as deleting the text from the file. As a result, an item exists, but that item is empty. This cmdlet works on an item with content, instead of items with values. The clc is the alias for this cmdlet.

Syntax

Parameters

-Path

The -Path parameter is used to specify the path to that item from which content is deleted. Wildcard characters are accepted. The path must be the path to the items, not to the containers. It is required, but the name of a parameter -Path is optional.

-LiteralPath

The -LiteralPath parameter is used to specify the paths to the items from which content is deleted. Unlike the -Path parameter, the value of it used exactly as it is typed. Wildcard characters are not accepted.

If the path includes the escape characters, enclose it in single quotation marks. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence.

-Force

The -Force parameter is used to force the command to execute without asking for user confirmation.

-WhatIf

The -WhatIf parameter displays what would happen if the cmdlet runs. The cmdlet does not execute.

-Filter

The -Filter parameter is used to specify a filter in the provider’s language or format. The value of it qualifies the -Path parameter. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having PowerShell filters the object after they’re accessed.

-Include

The content that this cmdlet clears specified as a string array. The value of -Include parameter qualifies the -¬Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted.

-Exclude

The content that this cmdlet omits from the path is specified as a string array. The value of it qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted.

-Confirm

The -Confirm parameter prompts a confirmation before running the cmdlet.

-Stream

The -Stream parameter is used to specify an alternate data stream for the content. If the stream does not exist, the clear-content cmdlet creates it. Wildcard characters are accepted.

It is a dynamic parameter that the Filesystem provider adds to the Clear-content. It works only in the file system drives.

Examples

Example 1: Delete the content from a specified file

PowerShell Clear-Content

The cmdlet, in this example, deletes the content from the File1.txt file in a specified location. The file is not deleted, but it is empty.

Example 2: Delete the content from all the files

PowerShell Clear-Content

The cmdlet, in this example, deletes the content of all the .txt files in the specified directory.