{"id":242,"date":"2017-05-12T13:30:46","date_gmt":"2017-05-12T18:30:46","guid":{"rendered":"https:\/\/www.indycloudcover.com\/?p=242"},"modified":"2018-04-26T09:35:56","modified_gmt":"2018-04-26T14:35:56","slug":"cleaning-up-files-with-powershell-gui-confirmation","status":"publish","type":"post","link":"https:\/\/www.indycloudcover.com\/?p=242","title":{"rendered":"Cleaning up files with powershell GUI confirmation"},"content":{"rendered":"<p>After automating my report last week, I decided I did not need a copy left lying around, so I decided to write a cleanup script.<\/p>\n<p>My first version was very simple, and consisted of just the required code to recurse the directory and delete the files.<\/p>\n<p>It occurred to me that a Yes\/No MsgBox to confirm the deletion would be a nice addition. So without further adieu, here is the code.<\/p>\n<pre class=\"toolbar:1 lang:ps decode:true \" ># Written By: Mark Gladson\r\n# \r\n#\r\n# Last update 05\/12\/2017 13:57\r\n\r\n# Get all Excel files in the specified location (Modified .xlsx - actual report output)\r\n$xclrpt = Get-ChildItem D:\\exports\\rvtools -recurse -Include *.xlsx\r\n$yy=$xclrpt.Count\r\n\r\n\r\n#List and delete the above found excel files\r\n \r\nWrite-Host \"Found $yy Excel files\" -ForegroundColor Cyan\r\n\r\n foreach ($xl in $xclrpt)\r\n {\r\n Write-Host \u201d \u201c$xl.Fullname -ForegroundColor Green\r\n \r\n }\r\n\r\n\r\n# add the required .NET assembly:\r\nAdd-Type -AssemblyName System.Windows.Forms\r\n\r\n# show the MsgBox:\r\n$delmsg = \"Delete \" + $yy +\" Files?\"\r\n$result = [System.Windows.Forms.MessageBox]::Show($delmsg, 'Warning', 'YesNo', 'Warning')\r\n\r\n# check the result:\r\nif ($result -eq 'Yes')\r\n{\r\n  \r\nWrite-Host \"Deleting \" $yy \"Files:\" -ForegroundColor Cyan\r\n\r\nforeach ($xl in $xclrpt)\r\n {\r\n \r\n Write-Host \u201d \u201c$xl.Fullname -ForegroundColor Magenta\r\n \r\n Remove-Item ($xl.FullName)\r\n\r\n }\r\n\r\n}\r\nelse\r\n{\r\n  $thewarning = \"Leaving \" + $yy + \" Files in place\"\r\n  Write-warning $thewarning\r\n\r\n}\r\n\r\n\r\n<\/pre>\n<p>And here is the output<\/p>\n<div id=\"attachment_246\" style=\"width: 247px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.indycloudcover.com\/wp-content\/uploads\/2017\/05\/msgbox.jpg\"><img aria-describedby=\"caption-attachment-246\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.indycloudcover.com\/wp-content\/uploads\/2017\/05\/msgbox.jpg\" alt=\"\" width=\"237\" height=\"162\" class=\"size-full wp-image-246\" \/><\/a><p id=\"caption-attachment-246\" class=\"wp-caption-text\">MsgBox output from script<\/p><\/div>\n<p>See how nice that looks?  You can use all the Windows GUI Framework in your scripts.  I will be designing a script next week that uses several parts of the windows framework to make the script more end user friendly.  Keep reading to see my progress and learn how to apply these techniques to your script!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After automating my report last week, I decided I did not need a copy left lying around, so I decided to write a cleanup script. My first version was very simple, and consisted of just the required code to recurse the directory and delete the files. It occurred to me that a Yes\/No MsgBox to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/posts\/242"}],"collection":[{"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=242"}],"version-history":[{"count":7,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=\/wp\/v2\/posts\/242\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.indycloudcover.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}