Reputation: 3521
I have the following script:
try {
Invoke-Command -Computer $Server -ScriptBlock {
param ($dir, $name)
Get-ChildItem -Path $dir |
Where {$_.Name -Match "$name"} |
Remove-Item -confirm:$false -Recurse -Verbose
} -ArgumentList $Directory, $DB
Write-Host "`r`nsuccessfull! " -foregroundcolor yellow -backgroundcolor black
}
catch {
write-host "`r`nFAILED!" -foregroundcolor red -backgroundcolor black
Write-Host "$($error[0])`r`n" -foregroundcolor magenta -backgroundcolor black
}
if no files exist to delete...it currently still says "successful" as output...can i refine this to say
"there was no files to delete" else "successfully deleted x number of files"
?
also, the recurse
on verbose is needed because otherwise i am forced to manually confirm even with confirm parameter. I think its because there are many sub items inside the target folder(s) i am looking to delete...
however, i get a TON of verbose messages for every single one of those items saying
VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem
VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem1
VERBOSE: Performing the operation "Remove Directory" on target \name1\subitem2
can i make it so that it just prints verbose on a folder level instead for every single item?
VERBOSE: Performing the operation "Remove Directory" on target \name1
Upvotes: 0
Views: 154
Reputation: 1248
Assign your file list to a variable that you can then inspect. Throwing everything through the pipeline is fine for quick-and-dirty, but not so much if you want to extract status information at various points.
$delfiles = Get-ChildItem -Path $dir | Where {$_.Name -Match "$name"}
If ($delfiles) {
Write-Host "There are $($delfiles.count) to delete "
$delfiles | Remove-Item -confirm:$false -Recurse #put a try/catch here maybe
Write-Host "`r`nsuccessful! "
}
else {
write-host "there were no files to delete"
}
Upvotes: 1