24/7/365 Support

Clear or Remove a File from PowerShell

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.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say