# Remove-Item help

**URL:** <https://forums.powershell.org/t/remove-item-help/2421>\
**Category:** PowerShell Help\
**Created:** [April 4, 2014, 6:50am UTC](https://forums.powershell.org/t/remove-item-help/2421 "2014-04-04T06:50:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [April 4, 2014, 6:50am UTC](https://forums.powershell.org/t/remove-item-help/2421/1 "2014-04-04T06:50:06Z")

</div>

`<#`

`
$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles(“\cd1001-c100\public\isantillan”, “.”, $searchOption);
foreach($fileName in $fileEntries)
{
$fileName | Out-File “D:\Backups\Pdrive cleanup\files_to_delete.txt” -Append
}
or
$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles(“\cd1001-c100\public\isantillan”, “.”, $searchOption);
$fileEntries | Out-File “D:\Backups\Pdrive cleanup\files_to_delete.txt”
`

`#>
`

[https://powershell.org/forums/topic/writeoutput-help/](https://powershell.org/forums/topic/writeoutput-help/)

Thanks to Don and Dave I was able to get the full UNC path of files in a network location. (see code above). Now with this text file, i will be deleting all the files that are listed using the code below. Everything works fine but i would like to log the files that are being deleted and in case if it fails for some reason, i’d be able to see which files and why. How do i output **Remove-Item** to text? Please advise.

`
Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" | Remove-Item
`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 4, 2014, 10:59am UTC](https://forums.powershell.org/t/remove-item-help/2421/2 "2014-04-04T10:59:00Z")

</div>

I’d probably do something along these lines:

```
Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" |
ForEach-Object {
    $path = $_

    try
    {
        Remove-Item -Path $path -ErrorAction Stop
        "'$path' Deleted Successfully"
    }
    catch
    {
        "Error deleting '$path': $($_.Exception.Message)"
    }
} |
Out-File -FilePath "D:\Backups\Pdrive cleanup\fileDeletion.log"
```

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [April 7, 2014, 4:06am UTC](https://forums.powershell.org/t/remove-item-help/2421/3 "2014-04-07T04:06:22Z")

</div>

[quote=14339]I’d probably do something along these lines:

```
Get-Content "D:\Backups\Pdrive cleanup\files_to_delete.txt" |
ForEach-Object {
    $path = $_

    try
    {
        Remove-Item -Path $path -ErrorAction Stop
        "'$path' Deleted Successfully"
    }
    catch
    {
        "Error deleting '$path': $($_.Exception.Message)"
    }
} |
Out-File -FilePath "D:\Backups\Pdrive cleanup\fileDeletion.log"
```

[/quote]

Hello Dave, I was able to get it it to output by using the code below. This script is to be run on the background so no status messages needs to be displayed.

`$timestamp = Get-Date -UFormat “%Y-%m-%d”
$getContent = Get-Content “D:\Backups\Pdrive cleanup\files_to_delete.txt” | Remove-Item -force -verbose *>> “D:\Backups\Pdrive cleanup\logs$timestamp.txt”`

It works perfectly fine with PowerShell 4.0. However, when I copied it to the server, I received the attached error. After some digging, I found out that it’s a version issue. The version that is currently installed on the server is PowerShell 2.0.

[http://technet.microsoft.com/en-us/library/hh847746.aspx](http://technet.microsoft.com/en-us/library/hh847746.aspx)  
NOTE: The All (\*), Warning (3), Verbose (4) and Debug (5) redirection operators were introduced  
in Windows PowerShell 3.0. They do not work in earlier versions of Windows PowerShell.

How do I get the same results using Powershell 2.0 with the code I provided above?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 7, 2014, 5:03am UTC](https://forums.powershell.org/t/remove-item-help/2421/4 "2014-04-07T05:03:07Z")

</div>

Do you need Verbose, Warning or Debug output in the log file, or only errors? If it’s errors only, you can use the 2\>\> operator in PowerShell 2.0 (instead of \*\>\> in your example).

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [April 7, 2014, 5:38am UTC](https://forums.powershell.org/t/remove-item-help/2421/5 "2014-04-07T05:38:44Z")

</div>

Unfortunately, it’s not only errors that i would like logged but everything that ‘Remove-Item’ does.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 7, 2014, 5:49am UTC](https://forums.powershell.org/t/remove-item-help/2421/6 "2014-04-07T05:49:11Z")

</div>

I don’t think I’m understanding your question, then. I suggested code which outputs information when Remove-Item successfully deletes something, and when it produces errors, but you went in a different direction with the redirection operators. Redirection would only give you verbose / debug / error / warning output, since Remove-Item doesn’t produce anything on the Output stream by itself.

---

<div class="post-metadata">

**Author:** ![ian-santillan](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@ian-santillan](https://forums.powershell.org/u/ian-santillan)\
**Post date:** [April 7, 2014, 5:57am UTC](https://forums.powershell.org/t/remove-item-help/2421/7 "2014-04-07T05:57:19Z")

</div>

I needed to see the what Remove-Item does. Which is why i used -verbose to which i used \*\>\> operator in order to capture it and output it to text. This is what i get in the text file **2014-04-07.txt**

Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\20amp.txt”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture1.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture2.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Capture3.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\PhotomergeUI.8BF”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\public\_drive\_log.txt”.  
Remove-Item : Cannot find path ‘\cd1001-c100\public\isantillan\Thumbs.db’ because it does not exist.  
At line:2 char:77

- $getContent = Get-Content “D:\Backups\Pdrive cleanup\files\_to\_delete.txt” | Remo …
- 

```
                                                                        ~~~~

```

  - CategoryInfo : ObjectNotFound: (\cd1001-c100\p…illan\Thumbs.db:String) [Remove-Item], ItemNotFoundException
  - FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture2.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture3.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture4.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\capture\Capture5.PNG”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder1\Robocopy.exe”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder2\Robocopy.exe”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\folder3\Robocopy.exe”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Copier\_unstoppable\UnstopCpy\_5\_2\_Win2K\_UP.exe”.  
Performing the operation “Remove File” on target “\cd1001-c100\public\isantillan\Copier\_unstoppable\UnstopCpy\_Configure.jpg”.  
Remove-Item : Cannot find path ‘\cd1001-c100\public\isantillan\Copier\_unstoppable\Thumbs.db’ because it does not exist.  
At line:2 char:77

- $getContent = Get-Content “D:\Backups\Pdrive cleanup\files\_to\_delete.txt” | Remo …
- 

```
                                                                        ~~~~

```

  - CategoryInfo : ObjectNotFound: (\cd1001-c100\p…pable\Thumbs.db:String) [Remove-Item], ItemNotFoundException
  - FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 7, 2014, 6:11am UTC](https://forums.powershell.org/t/remove-item-help/2421/8 "2014-04-07T06:11:05Z")

</div>

I see. I have a logging module which is compatible with PowerShell 2.0, and can be used to capture that Verbose output, over on the TechNet Gallery: [http://gallery.technet.microsoft.com/scriptcenter/Enhanced-Script-Logging-27615f85](http://gallery.technet.microsoft.com/scriptcenter/Enhanced-Script-Logging-27615f85) .

It may also be possible to make use of the System.Management.Automation.PowerShell class to run your command, which would get you access to all of the stream output from the commands executed by that PowerShell object. Other than those options, I don’t think there’s any other way you can intercept and log Verbose stream output prior to PowerShell 3.0.

---

<div class="post-metadata">

**Author:** ![tore-groneng](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tore-groneng/32/723_2.png) [@tore-groneng](https://forums.powershell.org/u/tore-groneng)\
**Post date:** [April 8, 2014, 11:25pm UTC](https://forums.powershell.org/t/remove-item-help/2421/9 "2014-04-08T23:25:24Z")

</div>

hi,

Will this work in powershell 2.0?. Add this function to you script. It should capture the calls to write-verbose. I do not have access to anything running powershell 2.0, so I cannot test it:

`
Function Global:Write-Verbose
{
Param(
        [string] $Message
)
Add-Content -Path .\verbose.log -Value $Message
}
`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 8, 2014, 11:32pm UTC](https://forums.powershell.org/t/remove-item-help/2421/10 "2014-04-08T23:32:35Z")

</div>

Hi Tore,

That works for intercepting explicit calls to the Write-Verbose cmdlet, but it would not catch verbose output from compiled cmdlets such as Remove-Item. That type of output goes through the [Cmdlet.WriteVerbose()](http://msdn.microsoft.com/en-us/library/system.management.automation.cmdlet.writeverbose(v=vs.85).aspx) method.

---

<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:52pm UTC](https://forums.powershell.org/t/remove-item-help/2421/11 "2024-05-16T20:52:13Z")

</div>


