views:

172

answers:

1

I'm implementing a custom PowerShell provider. I'm now working on the remove-item cmdlet implementation.

The RemoveItem method has the following signature:

protected override void RemoveItem(string path, bool recurse)

When I type: Remove-Item .\Myobject -recurse the PowerShell infrastructure provides me with the value true in the recurse parameter of the RemoveItem method.

However when I type: Remove-Item .\MyObject' I get a question:

The item at MyObject has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):

I guess this question is presented to my by the PowerShell infrastructure. This is perfectly fine because the object that I want to remove is a container. If I answer yes to the above question the PowerShell infrastructure does not set the recurse parameter. In fact it is false when my RemoveItem method is called. I would except the parameter to be true because I answered yes to the question.

My questions are:

  1. Why does PowerShell not set the bool recurse parameter to the correct value?

  2. Do I need to get the value (answer to the question) in some other way? How?

  3. If above is not possible is there a way to suppress the question?

+2  A: 

Ultimately if you're asked to remove a container then it is inherently recursive unless the container doesn't contain other containers. I believe PowerShell prompts because the action affects more than the user might initially be aware of (all the container's contents) and warrants confirmation. So in this case, I think the -recurse is used to tell PowerShell "I know what I'm doing".

Where -recurse makes more sense is if you were to execute something like this:

Remove-Item *.bak -recurse

In this case, you want to recursively search for files to delete. Unfortunately the Recurse parameter on Remove-Item is broken for this usage - from the docs:

Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet.

So the way to do this currently is:

Get-ChildItem . -r *.bak | Remove-Item
Keith Hill

related questions