Question:
I have the following command:
1 2 3 4 |
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:
1 2 3 |
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:
1 2 3 4 |
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?
Answer:
Using ForEach-Object
is pretty straightforward:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
$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' |