Clear-Content

 

Additional Resources for Clear-Content

 

Erasing the Contents of a File

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/clear-content.mspx

 

 

SYNOPSIS

Deletes the contents of an item, such as deleting the text from a file, but does not delete the item.

 

SYNTAX

Clear-Content [-path] <string[]> [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-force] [-credential <PSCredential>] [-whatIf] [-confirm] [<CommonParameters>]

 

Clear-Content [-literalPath] <string[]> [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-force] [-credential <PSCredential>] [-whatIf] [-confirm] [<CommonParameters>]

 

DETAILED DESCRIPTION

The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but does not delete the item. As a result, the item exists, but is empty. Clear-Content is similar to Clear-Item, but works on files, instead of aliases and variables.

 

PARAMETERS

 

-path <string[]>

Specifies the paths to the items from which content is deleted. Wildcards are permitted. The paths must be paths to items, not to containers. For example, you must specify a path to one more files, not a path to a directory. Wildcards are permitted. This parameter is required, but the parameter name ("-Path") is optional.

 

Required?

true

Position?

1

Default value

N/A - The path must be specified

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

true

 

-include <string[]>

Clears only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

true

 

-exclude <string[]>

Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

true

 

-filter <string>

Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-force <SwitchParameter>

Overrides restrictions that prevent the command from succeeding, just so the changes do not compromise security. For example, Force will override the read-only attribute or create directories to complete a file path, but it will not attempt to change file permissions.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-credential <PSCredential>

Uses a credential to validate access to the file. <Credential> represents a user-name, such as "User01" or "Domain01\User01", or a PSCredential object, such as the one retrieved by using the Get-Credential cmdlet. If you type a user name, you will be prompted for a password. This parameter appears, but it is not supported in any Windows PowerShell core cmdlets or providers.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-literalPath <string[]>

Specifies the paths to the items from which content is deleted. Unlike Path, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

true (ByPropertyName)

Accept wildcard characters? 

false

 

-whatIf

Describes what would happen if you executed the command without actually executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-confirm

Prompts you for confirmation before executing the command.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

INPUT TYPE

String

 

RETURN TYPE

None.

 

NOTES

 

For more information, type "Get-Help Clear-Content -detailed". For technical information, type "Get-Help Clear-Content -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

You can also refer to Clear-Content by its built-in alias, "clc". For more information, see About_Alias.

 

If you omit the -Path parameter name, the value of -Path must be the first parameter in the command. For example, "clear-content c:\mydir\*.txt" If you include the parameter name, you can list the parameters in any order.

 

You can use Clear-Content with the PowerShell File System provider and with other providers that manipulate content. To clear items that are not considered to be content, such as items managed by the PowerShell Certificate or Registry providers, use Clear-Item.

 

EXAMPLE 1

 

clear-content ..\SmpUsers\*\init.txt

 

This command deletes all of the content from the "init.txt" files in all subdirectories of the SmpUsers directory. The files are not deleted, but they are empty.

 

EXAMPLE 2

 

clear-content -path * -filter *.log -force

 

This command deletes the contents of all files in the current directory with the ".log" file name extension, including files with the read-only attribute. The asterisk (*) in the path represents all items in the current directory. The Force parameter makes the command effective on read-only files. Using a filter to restrict the command to files with the ".log" file name extension, instead of specifying "*.log" in the path makes the operation faster.

 

EXAMPLE 3

 

clear-content c:\Temp\* -Include Smp* -Exclude *2* -whatif

 

This command requests a prediction of what would happen if you submitted the command: "clear-content c:\temp\* -include smp* -exclude *2*". The result lists the files that would be cleared, in this case, files in the Temp directory whose names begin with "Smp", unless the file names include a "2". To execute the command, run it again without the Whatif parameter.

 

RELATED LINKS

Get-Content

Set-Content

Add-Content

Get-Item

about_namespace