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 confirm the deletion would be a nice addition. So without further adieu, here is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# Written By: Mark Gladson # # # Last update 05/12/2017 13:57 # Get all Excel files in the specified location (Modified .xlsx - actual report output) $xclrpt = Get-ChildItem D:\exports\rvtools -recurse -Include *.xlsx $yy=$xclrpt.Count #List and delete the above found excel files Write-Host "Found $yy Excel files" -ForegroundColor Cyan foreach ($xl in $xclrpt) { Write-Host ” “$xl.Fullname -ForegroundColor Green } # add the required .NET assembly: Add-Type -AssemblyName System.Windows.Forms # show the MsgBox: $delmsg = "Delete " + $yy +" Files?" $result = [System.Windows.Forms.MessageBox]::Show($delmsg, 'Warning', 'YesNo', 'Warning') # check the result: if ($result -eq 'Yes') { Write-Host "Deleting " $yy "Files:" -ForegroundColor Cyan foreach ($xl in $xclrpt) { Write-Host ” “$xl.Fullname -ForegroundColor Magenta Remove-Item ($xl.FullName) } } else { $thewarning = "Leaving " + $yy + " Files in place" Write-warning $thewarning } |
And here is the output
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!