Need to output command results to file

Jeff L 21 Reputation points
2021-09-10T17:48:45.273+00:00

Is there an easy way for me to pump the output of a series of file deletions using foreach?

Get-ChildItem -Path C:\Temp\Testing*\Beep* -Include * | foreach {$_.Delete()}

I know foreach doesn't support pipelining but I'm trying to keep this simple and am not well versed with the ForEach-Object command..

Thanks,

J

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,911 Reputation points
    2021-09-21T17:49:33.733+00:00

    You can't reference .Fullname in the catch because "$ underscore" will be the error object.

    Please use the Code Sample icon when posting code. The "101010" icon.

    Try it like this. I highly recommend using -whatif on the Remove-Item when you are testing.

    $Folders = Get-ChildItem 'C:\Users\*\AppData\Roaming\Temp*' -Recurse
    ""
    "Folders contains {0} entries." -f $Folders.count 
    ""
    $csv = ForEach ($File in $folders) {
        Try {
            Remove-Item $File.FullName -ErrorAction Stop -whatif 
            [PSCustomObject]@{
                Path = $File.FullName
                Action = "Removed"
                Error = ""
                }
            }
        Catch {
            [PSCustomObject]@{
                Path = $File.FullName
                Action = "NotRemoved"
                Error = $_ 
            }
        }
    } 
    
    $csv | Export-Csv C:\Temp\RR_Cleanup_Output.csv -NoTypeInformation
    

6 additional answers

Sort by: Most helpful
  1. Rich Matheisen 45,906 Reputation points
    2021-09-10T20:15:21.037+00:00

    A "ForEach" is a shortcut for the "ForEach-Object" cmdlet -- provided it's followed immediately by a block of code. Pipelining is usable.

    On the other hand, a "ForEach" followed immediately by an expression is parsed as a PowerShell keyword. There is no pipeline.

    This code should get you a report, and it will act in a predictable fashion if you encounter a file you can't remove. Also, the Get-ChildItem cmdlet will retrieve only files. The example you posted will also return directory names (but not the contents of the directory:

    Get-ChildItem C:\Temp\Testing*\Beep* -Include * -File | 
        ForEach-Object {
            $fn = $_.FullName
            Try {
                Remove-Item $_.FullName -ErrorAction Stop
                [PSCustomObject]@{
                    Path   = $_.FullName
                    Action = "Removed"
                    Error  = ""
                }
            }
            Catch {
                [PSCustomObject]@{
                    Path   = $fn
                    Action = "NotRemoved"
                    Error  = $_
                }
            }
        } | Export-Csv SomeFilePath -NoTypeInformation
    
    1 person found this answer helpful.
    0 comments No comments

  2. Philippe Levesque 5,711 Reputation points
    2021-09-10T18:34:16.197+00:00

    Hi

    The foreach seem ok, did you tried without the | foreach {$_.Delete()} to see if the commandlet Get-ChildItem -Path C:\Temp\Testing*\Beep* -Include * return what you need ?

    I tell that as the asterix in your path need a -Recurse to make it work, and the last asterix need a * if it's for the directory's content you search for.

    Get-ChildItem -Path C:\Temp\Testing*\Beep* -Include * -Recurse

    Thanks

    Philippe

    0 comments No comments

  3. Jeff L 21 Reputation points
    2021-09-10T19:16:43.037+00:00

    Hi Philippe,

    Yep, the command works/does what I want it to, I don't need the recurse as I'm not doing subdirectories AFAIK.. All I need is to get output from the command (all the files that were deleted) and dump it to a file.

    J

    0 comments No comments

  4. Jeff L 21 Reputation points
    2021-09-10T20:55:09.307+00:00

    Thanks Rich, that did it, appreciate your help.

    J

    0 comments No comments