PowerShell example of deleting and emptying the contents of a file using the clear-content command _powershell

Source: Internet
Author: User

This article describes how to remove or empty the contents of a file in PowerShell, use VBScript to create the FSO first, then open the file, and then modify the file; PowerShell empty the file as long as a clear-content.

The so-called empty file, that is, a text file inside the contents of all deleted, using Notepad and other tools to open, see the content is empty, the size of the file is also 0KB. Let's take a look at how the PowerShell is done in the empty file.

First, introduce a cmdlet, that is clear-content. First look at its basic introduction:

Copy Code code as follows:

Name
Clear-content
Summary
Deletes the contents of an item, such as deleting text from a file, but does not delete the item. (small part of a sentence: that is, only delete the file empty, do not delete the file itself.) )
Grammar
clear-content [-literalpath] [-credential] [-excl
Ude] [-filter] [-force] [-include] [-confirm
] [-whatif] [-usetransaction] []

clear-content [-path] [-credential] [-exclude ring[]>] [-filter] [-force] [-include] [-confirm] [-Wha
TIF] [-usetransaction] []


Small series: This syntax looks very complex, in fact, the simplest use is: clear-content < file path, other parameters are some control authority, some give an unprecedented hint, and some are completed bulk operation. These parameters can be understood when there is an in-depth requirement. Let's take a look at a simple use case:
Copy Code code as follows:

PS c:\users\splaybow> Echo hello,world! >d:\1.txt
PS c:\users\splaybow> Type D:\1.txt
Hello
world!
PS c:\users\splaybow> clear-content D:\1.txt
PS c:\users\splaybow> Type D:\1.txt
PS c:\users\splaybow>

Explain the meaning of this code, using echo to "hello,world!" Output to a new file "D:\1.txt" and then use Clear-content to empty the contents of the file. and the contents of the file are displayed when the Qing is unprecedented and empty.

About PowerShell use Clear-content to remove empty file content, this article on the introduction so much, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.