Cataster
Cataster

Reputation: 3541

How to limit recursive verbose output?

I have the following script that removes files and any folders matching the name. recurse is needed to avoid confirmation prompt.

Invoke-Command -Computer $Server -ScriptBlock { 
    param ($dir, $name)

    $f = Get-ChildItem -Path $dir | Where {$_.Name -Match "$name"}
    If ($f) {
        $f | Foreach {
            Remove-Item $_.fullname -confirm:$false -Recurse -Verbose 
        }
    }
    else {
        Write-Verbose "No file found"
    }
} -ArgumentList $Directory, $DB

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

VERBOSE: Performing the operation "Remove Directory" on target \name1.db

can i make it so that it just prints verbose on a folder level instead for every single subitem? essentially i would like only an output like this:

VERBOSE: Performing the operation "Remove Directory" on target \name1

VERBOSE: Performing the operation "Remove Directory" on target \name1.db

Upvotes: 1

Views: 366

Answers (1)

HAL9256
HAL9256

Reputation: 13473

Adding -Verbose to Remove-Item will always cause it to list out every item that it is removing (i.e. that's the point of Verbose output. It's a fire hose that's either on or off).

If you want, not necessarily less logging, but filtered logging, then the only real option is to remove -Verbose and do it yourself.

For ex.:

Invoke-Command -Computer $Server -ScriptBlock { 
    param ($dir, $name)

    $f = Get-ChildItem -Path $dir | Where {$_.Name -Match "$name"}
    If ($f) {
        $f | Foreach {
            #Check to see if it is a folder
            if($_.PSIsContainer)
            {
                Write-Host "Removing Directory: $($_.FullName)"
            }
            Remove-Item $_.fullname -confirm:$false -Recurse
        }
    }
    else {
        Write-Verbose "No file found"
    }
} -ArgumentList $Directory, $DB

Upvotes: 1

Related Questions