powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Clear-Content



NAME
Clear-Content

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

SYNTAX
Clear-Content [-LiteralPath] [-Credential ] [-Exclude ] [-Filter ] [-For
ce] [-Include ] [-Confirm] [-WhatIf] [-UseTransaction] []

Clear-Content [-Path] [-Credential ] [-Exclude ] [-Filter ] [-Force] [-I
nclude ] [-Confirm] [-WhatIf] [-UseTransaction] []


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


PARAMETERS
-Credential
Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated
by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers installed with Windows PowerShell.

Required? false
Position? named
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

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

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

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

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Force []
Allows the cmdlet to clear the file contents even if the file is read-only. Implementation varies from provider
to provider. For more information, see about_Providers. Even using the Force parameter, the cmdlet cannot over
ride security restrictions.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Include
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? false

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

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

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

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
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

-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

-UseTransaction []
Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
For more information, see about_Transactions.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false


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

INPUTS
None
You cannot pipe objects to Clear-Content.


OUTPUTS
None
This cmdlet does not return any objects.


NOTES


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

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 Windows PowerShell File System provider and with other providers that manipu
late content. To clear items that are not considered to be content, such as items managed by the Windows PowerS
hell Certificate or Registry providers, use Clear-Item.

The Clear-Content cmdlet is designed to work with the data exposed by any provider. To list the providers avail
able in your session, type "Get-PsProvider". For more information, see about_Providers.


-------------------------- EXAMPLE 1 --------------------------

C:\PS>clear-content ..\SmpUsers\*\init.txt


Description
-----------
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 --------------------------

C:\PS>clear-content -path * -filter *.log -force


Description
-----------
This command deletes the contents of all files in the current directory with the ".log" file name extension, includ
ing 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 file
s with the ".log" file name extension instead of specifying "*.log" in the path makes the operation faster.





-------------------------- EXAMPLE 3 --------------------------

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


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






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113282
about_Providers
Get-Content
Set-Content
Add-Content
Get-Item