7

I have the following command:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | Remove-Item -Verbose

to clear some files from the build folder of a VS solution. I use the Verbose switch so that I can see which files are being deleted. It works fine but the output is too verbose:

VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.mvc3.readme.txt".
VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.readme.txt".

I just need to see something like that:

Removing file \App_Readme\glimpse.mvc3.readme.txt".
Removing file \App_Readme\glimpse.readme.txt".
...

I know i can do this with a foreach statement and a Write-Host command, but I believe it can be done with some pipelining or something. Any ideas?

lalibi
  • 3,057
  • 3
  • 33
  • 41

4 Answers4

10

Using ForEach-Object is pretty straightforward:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | foreach{ "Removing file $($_.FullName)"; Remove-Item $_}

As @user978511 pointed out, using the verbose output is more complicated:

$ps = [PowerShell]::Create()

$null = $ps.AddScript(@'
    Get-ChildItem $build_path `
        -Include *.bak, *.orig, *.txt, *.chirp.config `
        -Recurse | Remove-Item -Verbose
'@)

$ps.Invoke()
$ps.Streams.Verbose -replace '(.*)Target "(.*)"(.*)','Removing File $2'
Rynant
  • 23,153
  • 5
  • 57
  • 71
4

This is a few years too late but it might help someone else who stumbles upon this like I did so I'll provide it anyway.

I would try the file deletion and then report on success or failure. See below:

$FileList = $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse

foreach ($File in $FileList)
{
    Try
    {
        Remove-Item $File.FullName -Force -ErrorAction Stop
        Write-Output "Deleted: $($File.Parent)\$($File.Name)"
    }
    Catch
    {
        Write-Output "Error deleting: $($File.Parent)\$($File.Name); Error Message: $($_.Exception.Message)"
    }
}

If you wanted to both output to console and log to a file you can use Tee-Object at the end of each line starting with Write-Output above.

| Tee-Object -FilePath $your_log_file -Append
Matt R
  • 398
  • 2
  • 5
4

In PowerShell 3.0 you can write the Verbose stream to the output stream (e.g 4>&1) and then replace the message:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | Remove-Item -Verbose 4>&1 | Foreach-Object{ `
        Write-Host ($_.Message -replace'(.*)Target "(.*)"(.*)','Removing File $2') -ForegroundColor Yellow
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
1

To be able to modify the message you need first to cath the output that is not that easy. You can refer to answer on this page: Powershell Invoke-Sqlcmd capture verbose output to catch the output. From there on you can modify the message and show it in your format, but foreach options looks easier to me

Community
  • 1
  • 1
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52