# Delete specific sub folders from a folder and create a log file of the operations done

**URL:** <https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219>\
**Category:** PowerShell Help\
**Created:** [April 9, 2021, 8:32pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219 "2021-04-09T20:32:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [April 9, 2021, 8:32pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219/1 "2021-04-09T20:32:42Z")

</div>

Hello there,

I am trying to delete specific folders contained in C:\temp but not all, I have listed them in a text file.

> $list = gc C:\tmp\list.txt  
> foreach($item in $list){
> 
> Remove-Item “C:\tmp$($item)” -Recurse -Verbose
> 
> }

I tried to save the code as test.ps1 and did below, and saved the verbose stream to log

> .\test.ps1 3\>&1 2\>&1 | Out-file log.txt

Is there any other way to delete and save the log at once in a script itself?

---

<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:** [April 9, 2021, 8:54pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219/2 "2021-04-09T20:54:30Z")

</div>

So your code seem to work, right. What’s wrong with it? Why do you want another way?

You could add some code into the script to log a little more explicitly. When you put something like this inside the loop for example:

```auto
"Removing folder '$(C:\tmp$($item))'" | Out-File -FilePath .\log.txt -Append

```

---

<div class="post-metadata">

**Author:** ![Rahul20](https://avatars.discourse-cdn.com/v4/letter/r/838e76/32.png) [@Rahul20](https://forums.powershell.org/u/Rahul20)\
**Post date:** [April 9, 2021, 9:15pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219/3 "2021-04-09T21:15:23Z")

</div>

Thanks @Olaf . tried this , but getting an error as File can’t be opened as it is used by another process.Code is mentioned below:

> function cln-tmp{
> 
> $list = gc C:\tmp\list1.txt  
> $i= 1 ;  
> $results = foreach($item in $list){  
> Write-Host “`n $($i). Working with [C:\tmp$($item)]…”
> 
> “`n $($i). Removing folder ‘(C:\tmp$($item))’” | Out-File -FilePath c:\tmp\log.txt -Append
> 
> Remove-Item “C:\tmp$($item)” -Recurse -Verbose
> 
> $i++
> 
> }
> 
> }
> 
> cln-tmp 3\>&1 2\>&1 4\>&1 6\>&1|Out-File c:\tmp\log.txt

So, I would like to save the folder that I am working on (can be write-host command) and the verbose results, how can I save all together to a text 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:** [April 9, 2021, 10:01pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219/4 "2021-04-09T22:01:36Z")

</div>

Now you’re trying to write the log from inside the script AND from outside the script - that’s why you get an error. You will have to either choose one the ways or use different files inside and outside.

---

<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:26pm UTC](https://forums.powershell.org/t/delete-specific-sub-folders-from-a-folder-and-create-a-log-file-of-the-operations-done/16219/5 "2024-05-16T20:26:49Z")

</div>


