{"id":4319,"date":"2015-04-02T11:00:13","date_gmt":"2015-04-02T15:00:13","guid":{"rendered":"http:\/\/jdhitsolutions.com\/blog\/?p=4319"},"modified":"2015-04-04T11:30:28","modified_gmt":"2015-04-04T15:30:28","slug":"powershell-blogging-week-supporting-whatif-and-confirm","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/","title":{"rendered":"PowerShell Blogging Week: Supporting WhatIf and Confirm"},"content":{"rendered":"<p>We hope you are enjoying this experiment in community blogging. In today's contribution I want to demonstrate how you can add support for WhatIf and Confirm to your advanced PowerShell functions. It is actually quite easy, especially if your function is simply calling other PowerShell commands that already support \u2013Whatif and \u2013Confirm. The recommended best practice is that if your function will do anything that changes something, it should support these parameters. Here's how.<\/p>\n<p>In your function you will need to use the cmdletbinding attribute and specify SupportsShouldProcess.<\/p>\n<pre><code>[cmdletbinding(SupportsShouldProcess)]<\/code><\/pre>\n<p>Beginning with PowerShell 3.0 this is all you need but you will see scripters explicitly setting this to $True.<\/p>\n<pre><code>[cmdletbinding(SupportsShouldProcess=$True)]<\/code><\/pre>\n<p>That's fine, although personally I find it redundant. If SupportsShouldProcess is listed then by default it is True. There is no need to explicitly set this to $False. Simply omit it. When you add this attribute, you will automatically get the \u2013WhatIf and \u2013Confirm parameters. The best part is that if your function is simply calling PowerShell cmdlets that already support \u2013WhatIf, they will automatically inherit this setting. Here's a sample function.<\/p>\n<pre class=\"lang:ps decode:true \" >#requires \u2013version 4.0\r\n\r\nFunction Remove-TempFile {\r\n[cmdletbinding(SupportsShouldProcess)]\r\n\r\nParam(\r\n[Parameter(Position=0)]\r\n[ValidateScript({Test-Path $_})]\r\n[string]$Path = $env:temp\r\n)\r\n\r\n#get last bootup time\r\n$LastBoot = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUptime\r\nWrite-Verbose \"Finding all files in $path modified before $lastboot\"\r\n\r\n(Get-Childitem -path $path -File).Where({$_.lastWriteTime -le $lastboot}) | Remove-Item\r\n\r\n} #end function\r\n\r\n<\/pre>\n<p>The function deletes all files from the %TEMP% folder that have a last modified time older than the last boot up time. As you can see in the help, PowerShell added the necessary parameters.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png\" alt=\"\" \/><\/p>\n<p>When I run the function with \u2013Whatif it is passed on to Remove-Item.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB2.png\" alt=\"\" \/><\/p>\n<p>It is really that easy. I also automatically get support for \u2013Confirm.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB3.png\" alt=\"\" \/><\/p>\n<p>Things gets a little trickier when you want to support WhatIf for a function where your commands don't natively recognize SupportsShouldProcess. This would be true of any .NET static method or even a command line tool you might be running, to name a few examples. To add your own support you need to invoke the built-in $PSCmdlet object and its ShouldProcess() method. Here's a simple example.<\/p>\n<pre class=\"lang:ps decode:true \" >Function Set-Folder {\r\n[cmdletbinding(SupportsShouldProcess)]\r\n\r\nParam(\r\n[Parameter(Position=0,\r\nValueFromPipeline,\r\nValueFromPipelineByPropertyName)]\r\n[Alias(\"pspath\")]\r\n[ValidateScript({Test-Path $_})]\r\n[string]$Path=\".\")\r\n\r\nProcess {\r\n$Path = (Resolve-Path -Path $Path).ProviderPath\r\nif ($PSCmdlet.ShouldProcess($Path)) {\r\n#do the action\r\n$Path.ToUpper()\r\n}\r\n} #Process\r\n\r\n} #end function\r\n\r\n<\/pre>\n<p>This function hypothetically is going to perform some action on a folder and I'm simply displaying the folder name in upper case. The important part is the If statement. This is the bare minimum that you need. If you specify \u2013WhatIf you'll be prompted.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB4.png\" alt=\"\" \/><\/p>\n<p>The operation will be the name of your script or function. The target is the ShouldProcess parameter value which in my example is the path. But you can provide more specific information by specifying ShouldProcess parameters for the target and action. Here's a revised function.<\/p>\n<pre class=\"lang:ps decode:true \" >Function Set-Folder2 {\r\n[cmdletbinding(SupportsShouldProcess)]\r\n\r\nParam(\r\n[Parameter(Position=0,\r\nValueFromPipeline,\r\nValueFromPipelineByPropertyName)]\r\n[Alias(\"pspath\")]\r\n[ValidateScript({Test-Path $_})]\r\n[string]$Path=\".\")\r\n\r\nProcess {\r\n$Path = (Resolve-Path -Path $Path).ProviderPath\r\nif ($PSCmdlet.ShouldProcess($Path,\"Updating\")) {\r\n#do the action\r\n$Path.ToUpper()\r\n}\r\n} #Process\r\n\r\n} #end function\r\n\r\n<\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB5.png\" alt=\"\" \/><\/p>\n<p>You must have the code for ShouldProcess otherwise even if you set the cmdletbinding attribute, PowerShell won't know which commands need WhatIf. You can also have as many ShouldProcess statements as you need.<\/p>\n<p>When it comes to confirmation, things get a little trickier and it might depend on what you really need. As you saw above, any cmdlet that supports \u2013Confirm should automatically inherit the setting. This works because there is another cmdletbinding attribute called ConfirmImpact which has a default value of Medium. Other options are Low and High. My first function could also have been written like this:<\/p>\n<pre><code>[cmdletbinding(SupportsShouldProcess,ConfirmImpact=\"medium \")]<\/code><\/pre>\n<p>Confirmation happens by comparing the value of ConfirmImpact with the built-in $ConfirmPreference variable which has a default value of High. If the value of $ConfirmPreference is equal to or greater than ConfirmImpact, PowerShell will prompt for confirmation. Let's test this out.<\/p>\n<pre class=\"lang:ps decode:true \" >Function Set-Folder6 {\r\n[cmdletbinding(SupportsShouldProcess,ConfirmImpact=\"High\")]\r\n\r\nParam(\r\n[Parameter(Position=0,\r\nValueFromPipeline,\r\nValueFromPipelineByPropertyName)]\r\n[Alias(\"pspath\")]\r\n[ValidateScript({Test-Path $_})]\r\n[string]$Path=\".\"\r\n)\r\nBegin {\r\nWrite-Verbose \"Starting $($MyInvocation.Mycommand)\"\r\n} #begin\r\n\r\nProcess {\r\n$Path = (Resolve-Path -Path $Path).ProviderPath\r\nWrite-Verbose \"Processing $path\"\r\nif ($PSCmdlet.ShouldProcess($Path,\"Updating\")) {\r\n#do the action\r\n$Path.ToUpper()\r\n} #ShouldProcess\r\n} #Process\r\n\r\nEnd {\r\nWrite-Verbose \"Ending $($MyInvocation.Mycommand)\"\r\n} #end\r\n\r\n} #end function\r\n\r\n<\/pre>\n<p>Notice that I am also using for WhatIf. In this function the ConfirmImpact is set to high which means PowerShell will always prompt.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB6.png\" alt=\"\" \/><\/p>\n<p>If I edit the function and change to ConfirmImpact to Medium or Low, then PowerShell will only confirm if I ask.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB7.png\" alt=\"\" \/><\/p>\n<p>You don't have to specify anything for cmdletbinding. If you know you always want confirmation you can do something like this:<\/p>\n<pre class=\"lang:ps decode:true \" >Function Set-Folder4 {\r\n[cmdletbinding()]\r\n\r\nParam(\r\n[Parameter(Position=0,\r\nValueFromPipeline,\r\nValueFromPipelineByPropertyName)]\r\n[Alias(\"pspath\")]\r\n[ValidateScript({Test-Path $_})]\r\n[string]$Path=\".\",\r\n[switch]$Force\r\n)\r\n\r\nProcess {\r\n$Path = (Resolve-Path -Path $Path).ProviderPath\r\nWrite-Verbose \"Processing $path\"\r\nif ($Force -OR $PSCmdlet.ShouldContinue(\"Do you want to continue modifying folder?\",$path)) {\r\n#do the action\r\n$Path.ToUpper()\r\n}\r\n} #Process\r\n\r\n} #end function\r\n\r\n<\/pre>\n<p>Notice the use of the ShouldContinue method. When I run this function, PowerShell will always prompt for confirmation.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB8.png\" alt=\"\" \/><\/p>\n<p>I also added a switch parameter called Force so that if it is specified, the user is not prompted for confirmation.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB9.png\" alt=\"\" \/><\/p>\n<p>The downside to this approach is that help doesn't show anything.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB10.png\" alt=\"\" \/><\/p>\n<p>Perhaps in special cases this is what you want. Personally, I think you are better off using the cmdletbinding attributes as I did for my Set-Folder6 example.<\/p>\n<p>Adding support for WhatIf and Confirm doesn't take much effort and it will take your advanced function to the next level.<\/p>\n<hr>\n<p>This post is part of the PowerShell Blogging Week series on Windows PowerShell Advanced Functions, a series of coordinated posts designed to provide a comprehensive view of a particular topic.<\/p>\n<p>Other articles in this series:<\/p>\n<ul>\n<li><em><a href=\"http:\/\/www.lazywinadmin.com\/2015\/03\/standard-and-advanced-powershell.html\" target=\"_blank\">Standard and Advanced Functions<\/a><\/em> by <a href=\"https:\/\/twitter.com\/LazyWinAdm\" target=\"_blank\">Francois-Xavier Cat<\/a><\/li>\n<li><a href=\"http:\/\/mikefrobbins.com\/2015\/03\/31\/powershell-advanced-functions-can-we-build-them-better-with-parameter-validation-yes-we-can\/\"><em>PowerShell Advanced Functions: Can we build them better?<\/em><\/a> by <a href=\"https:\/\/twitter.com\/mikefrobbins\" target=\"_blank\">Mike F. Robbins<\/a><\/li>\n<li><a href=\"http:\/\/www.adamtheautomator.com\/psbloggingweek-dynamic-parameters-and-parameter-validation\" target=\"_blank\"><em>Dynamic Parameters and Parameter Validation<\/em><\/a> by <a href=\"https:\/\/twitter.com\/adbertram\">Adam Bertram<\/a><\/li>\n<li><em><a href=\"http:\/\/www.sapien.com\/blog\/2015\/04\/03\/advanced-help-for-advanced-functions\/\" target=\"_blank\">Creating Help and Comments<\/a><\/em> by <a href=\"https:\/\/twitter.com\/juneb_get_help\" target=\"_blank\">June Blender<\/a><\/li>\n<li><em><a href=\"http:\/\/learn-powershell.net\/2015\/04\/04\/a-look-at-trycatch-in-powershell\/\" target=\"_blank\">Try\/Catch and Essential Error Handling<\/a><\/em> by <a href=\"https:\/\/twitter.com\/proxb\" target=\"_blank\">Boe Prox<\/a><\/li>\n<\/ul>\n<p>We hope you found our work worth your time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We hope you are enjoying this experiment in community blogging. In today&#8217;s contribution I want to demonstrate how you can add support for WhatIf and Confirm to your advanced PowerShell functions. It is actually quite easy, especially if your function is simply calling other PowerShell commands that already support \u2013Whatif and \u2013Confirm. The recommended best&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Supporting WhatIf and Confirm in Advanced Functions #PowerShell #PSBlogWeek","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[32,534,490,540],"class_list":["post-4319","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-functions","tag-powershell","tag-psblogweek","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"We hope you are enjoying this experiment in community blogging. In today&#039;s contribution I want to demonstrate how you can add support for WhatIf and Confirm to your advanced PowerShell functions. It is actually quite easy, especially if your function is simply calling other PowerShell commands that already support \u2013Whatif and \u2013Confirm. The recommended best...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2015-04-02T15:00:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-04-04T15:30:28+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"PowerShell Blogging Week: Supporting WhatIf and Confirm\",\"datePublished\":\"2015-04-02T15:00:13+00:00\",\"dateModified\":\"2015-04-04T15:30:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/\"},\"wordCount\":803,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/032515_1609_PowerShellB1.png\",\"keywords\":[\"functions\",\"PowerShell\",\"PSBlogWeek\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/\",\"name\":\"PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/032515_1609_PowerShellB1.png\",\"datePublished\":\"2015-04-02T15:00:13+00:00\",\"dateModified\":\"2015-04-04T15:30:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/032515_1609_PowerShellB1.png\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2015\\\/03\\\/032515_1609_PowerShellB1.png\",\"width\":699,\"height\":251},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/4319\\\/powershell-blogging-week-supporting-whatif-and-confirm\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Blogging Week: Supporting WhatIf and Confirm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator","og_description":"We hope you are enjoying this experiment in community blogging. In today's contribution I want to demonstrate how you can add support for WhatIf and Confirm to your advanced PowerShell functions. It is actually quite easy, especially if your function is simply calling other PowerShell commands that already support \u2013Whatif and \u2013Confirm. The recommended best...","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/","og_site_name":"The Lonely Administrator","article_published_time":"2015-04-02T15:00:13+00:00","article_modified_time":"2015-04-04T15:30:28+00:00","og_image":[{"url":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"PowerShell Blogging Week: Supporting WhatIf and Confirm","datePublished":"2015-04-02T15:00:13+00:00","dateModified":"2015-04-04T15:30:28+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/"},"wordCount":803,"commentCount":7,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png","keywords":["functions","PowerShell","PSBlogWeek","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/","name":"PowerShell Blogging Week: Supporting WhatIf and Confirm &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#primaryimage"},"thumbnailUrl":"http:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png","datePublished":"2015-04-02T15:00:13+00:00","dateModified":"2015-04-04T15:30:28+00:00","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2015\/03\/032515_1609_PowerShellB1.png","width":699,"height":251},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4319\/powershell-blogging-week-supporting-whatif-and-confirm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"PowerShell Blogging Week: Supporting WhatIf and Confirm"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1869,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/1869\/add-whatif-support-to-your-powershell-scripts\/","url_meta":{"origin":4319,"position":0},"title":"Add WhatIf Support to Your PowerShell Scripts","author":"Jeffery Hicks","date":"December 2, 2011","format":false,"excerpt":"In one of my recent articles for SMB IT, I included a PowerShell module. In the article I referenced that I included support for -Whatif in one of the functions. I was asked on Twitter to explain what I meant and how it works. So here goes. There are a\u2026","rel":"","context":"In &quot;PowerShell v2.0&quot;","block_context":{"text":"PowerShell v2.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-v2-0\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8424,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8424\/hiding-taskbar-search-with-powershell\/","url_meta":{"origin":4319,"position":1},"title":"Hiding TaskBar Search with PowerShell","author":"Jeffery Hicks","date":"May 21, 2021","format":false,"excerpt":"Yesterday I shared a few PowerShell functions for configuring the Windows 10 taskbar to auto-hide. This works great in my virtual desktop when recording my Pluralsight courses. But even when hidden I would still get an annoying white sliver from the search box. So I got rid of that as\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/show-searchbox.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/show-searchbox.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/show-searchbox.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/show-searchbox.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":8420,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8420\/managing-the-windows-10-taskbar-with-powershell\/","url_meta":{"origin":4319,"position":2},"title":"Managing the Windows 10 Taskbar with PowerShell","author":"Jeffery Hicks","date":"May 20, 2021","format":false,"excerpt":"When I'm working on a Pluralsight course, I tend to setup a virtual machine for recording. Although, lately I've been trying with Windows 10 Sandbox. This is handy when all I need is a Windows 10 desktop. When I setup the system, I have particular settings I need to configure.\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/taskbar-regsettings.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/taskbar-regsettings.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/taskbar-regsettings.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/05\/taskbar-regsettings.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3912,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3912\/friday-fun-a-random-powershell-console\/","url_meta":{"origin":4319,"position":3},"title":"Friday Fun: A Random PowerShell Console","author":"Jeffery Hicks","date":"July 11, 2014","format":false,"excerpt":"This week I thought we'd have a little fun with the PowerShell console and maybe pick up a few scripting techniques along the way. Today I have a function that changes the foreground and background colors of your PowerShell console to random values. But because you might want to go\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"crayons","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/11\/crayons-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4268,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/4268\/friday-fun-size-me-up\/","url_meta":{"origin":4319,"position":4},"title":"Friday Fun: Size Me Up","author":"Jeffery Hicks","date":"March 6, 2015","format":false,"excerpt":"Part of day job involves creating training material, often in the form of video training for Pluralsight or articles for Petri.com. Since I usually am covering PowerShell I often need to capture a PowerShell session. And sometimes I want the screen to be a particular size. So over time I've\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8612,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8612\/update-registry-os-productname-with-powershell\/","url_meta":{"origin":4319,"position":5},"title":"Update Registry OS ProductName with PowerShell","author":"Jeffery Hicks","date":"October 12, 2021","format":false,"excerpt":"I expect many of you are like me and have done, or will do, an in-place upgrade from Windows 10 to Windows 11. It is easy enough to run a PowerShell expression like this to see the operating system name. Get-CimInstance win32_operatingsystem | Select-Object -property Caption I get a value\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/set-psproduct2.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/set-psproduct2.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/set-psproduct2.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/10\/set-psproduct2.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4319"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4319\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}