Problem
You want to clear the content of a file, or remove that file altogether.
Solution
To clear the content from a file, use the ClearContent cmdlet. Use the RemoveItem cmdlet to remove that file altogether, as shown by Example 171.
Example 171. Clearing content from and removing a file
PS >GetContent test.txt Hello World PS >ClearContent test.txt PS >GetContent test.txt PS >GetItem test.txt
Directory: Microsoft.PowerShell.Core\FileSystem::C:\temp
Mode
LastWriteTime
Length Name
a
4/23/2007
8:05 PM
0 test.txt
PS >RemoveItem test.txt PS >GetItem test.txt GetItem : Cannot find path 'C:\temp\test.txt' because it does not exist. At line:1 char:9
+ GetItem <<<< test.txt
Discussion
The (aptly named) ClearContent and RemoveItem cmdlets clear the content from an item and remove an item, respectively. Although the solution demonstrates this only for files in the filesystem, they in fact apply to any PowerShell providers that support the concepts of “content” and “items.” Examples of other drives that support these content and item concepts are the Function:, Alias:, and Variable:. The HKLM:, HKCU:, and Env: drives do not support the concept of content, but do let you remove items with the RemoveItem cmdlet.
The RemoveItem cmdlet has a handful of standard aliases: ri, rm, rmdir, del, erase, and rd.
For more information about the RemoveItem or ClearContent cmdlets, type GetHelp RemoveItem or GetHelp ClearContent.