# Using PowerShell clear-content issue

**URL:** <https://forums.powershell.org/t/using-powershell-clear-content-issue/18132>\
**Category:** PowerShell Help\
**Created:** [December 8, 2021, 3:53pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132 "2021-12-08T15:53:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@Ryan\_Kohn](https://forums.powershell.org/u/Ryan_Kohn)\
**Post date:** [December 8, 2021, 3:53pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/1 "2021-12-08T15:53:51Z")

</div>

I have a script that I want to run every night. It will create an Excel file (.xlsx) but I want to clear the contents before writing new content to it. I don’t have a problem using clear-content, but when I try to open the file afterwards, It says I cannot open due to the file format or file extension not being valid. Does anyone know what I can do or what is happening?

 ![08-12-2021 09_52-Excel](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/7/757a185730f74378e4ccb0f4dd6f49b29aca6157.jpeg)

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [December 8, 2021, 4:29pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/2 "2021-12-08T16:29:54Z")

</div>

Hi, welcome to the forum 👋

An Excel file is a container that stores not just the contents of the document but lots of data about how it’s structured. Change the extension to .ZIP and you’ll see it’s like browsing a folder or or archive file.

When you run `Clear-Content` you’re not just deleting the information on the spreadsheet, you’re actually deleting all the content that makes it an Excel file.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 8, 2021, 5:06pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/3 "2021-12-08T17:06:17Z")

</div>

Ryan,  
Welcome to the forum. 👋🏽

`Clear-Content` will not work properly for binary file formats like \*.XLSX or \*.DOCX. But if you create a new file in some way it should be empty be default anyway.  
To make your life easier you could try to work with the great module from Doug Finke

> **[ImportExcel 7.8.0](https://www.powershellgallery.com/packages/ImportExcel/7.8.0)**
>
> PowerShell module to import/export Excel spreadsheets, without Excel.
> Check out the How To Videos https://www.youtube.com/watch?v=U3Ne\_yX4tYo&list=PL5uoqS92stXioZw-u-ze\_NtvSo0k0K0kq

---

<div class="post-metadata">

**Author:** ![Ryan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@Ryan\_Kohn](https://forums.powershell.org/u/Ryan_Kohn)\
**Post date:** [December 8, 2021, 5:20pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/4 "2021-12-08T17:20:11Z")

</div>

Thank you both for the welcoming and also for your answers! I did not know that the command would clear the format of the Excel file. That won’t be very useful for me. I am actually not creating a new file either because I am using Power Automate to grab information from the Excel file. If I delete the file, Power Automate doesn’t like that and won’t fill in the table information for the specified file. I did read though, by default, out-file will overwrite the existing file. So I am playing around with that idea. Thank you both!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 8, 2021, 5:41pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/5 "2021-12-08T17:41:29Z")

</div>

> [@Ryan\_Kohn](#):
>
> out-file

Out-File outputs strings or string representations of objects in plain text to a text file.

If you want to use a pure PowerShell approach to create or manipulate \*.XLSX files you should take a look the module I linked in my last answer. You don’t even need an installed Excel for it. You could run it on a server from a scheduled task for example.

---

<div class="post-metadata">

**Author:** ![Ryan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@Ryan\_Kohn](https://forums.powershell.org/u/Ryan_Kohn)\
**Post date:** [December 8, 2021, 5:59pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/6 "2021-12-08T17:59:38Z")

</div>

Thank you. I did look up export-excel and then ran a get-help on it. There are a lot of things to do with that I think will help me.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 8, 2021, 6:22pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/7 "2021-12-08T18:22:49Z")

</div>

The help for the module has a lot of room for improvement unfortunately. But if you need some more help you could watch this:

[![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/6/6a8e01b7b7bf1d7ebad8f63c436bb8a64cf467ce.jpeg "ImportExcel PowerShell + Excel = Better Together! with Doug Finke") ](https://www.youtube.com/watch?v=0bR-8b6OMTk)

---

<div class="post-metadata">

**Author:** ![Ryan\_Kohn](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@Ryan\_Kohn](https://forums.powershell.org/u/Ryan_Kohn)\
**Post date:** [December 8, 2021, 7:07pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/8 "2021-12-08T19:07:24Z")

</div>

That guy has a lot of good stuff! I’ve been watching. Thanks for the resources!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/using-powershell-clear-content-issue/18132/9 "2024-05-16T20:19:58Z")

</div>


