Using Confirm only when specific parameters are specified

I have a function that includes a parameter to delete an object if it already exists. If the object does not exist or the user does not specify the parameter to delete the object, the function has no need to be confirmed. Only if the user specifies the parameter AND the object exists should the user be prompted to confirm. In that case, I would like the user to be able to specify the common -Confirm:$false parameter to avoid the prompt. From what I’ve read, it seems that Confirm along with ConfirmPreference only works at the function level. Is that really the case? If so, I have some not-so-elegant ideas about how to deal with it, but I’m up for other ideas. Thanks.

I would separate the tasks out into separate functions. One to get and one to remove. You can then pipe the get to the remove and have the ConfirmImpact in the cmdlet doing the change.

The remove one should include this at the top of the function.


Thanks, that’s what I ended up doing.