Using Confirm only when specific parameters are specified

This topic contains 1 reply, has 2 voices, and was last updated by  Alex Aymonier 1 day, 11 hours ago.

  • Author
  • #96338

    Hugh Martin

    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.

  • #101094

    Alex Aymonier

    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.


You must be logged in to reply to this topic.