{"id":79055,"date":"2016-06-28T00:01:03","date_gmt":"2016-06-28T07:01:03","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=79055"},"modified":"2019-02-18T09:10:33","modified_gmt":"2019-02-18T16:10:33","slug":"erase-files-based-on-date-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/erase-files-based-on-date-by-using-powershell\/","title":{"rendered":"Erase files based on date by using PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Honorary Scripting Guy, Sean Kearney, relates his first experience with PowerShell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I was curious just how difficult it is to use Windows PowerShell. For example, how difficult is it to erase files based on the current date?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> Honorary Scripting Guy, Sean Kearney, is here today, and I can completely relate to exactly what you\u2019ve said. I once had absolutely <u>zero<\/u> time to learn about scripting or automation. I was a field technician.<\/p>\n<p>You know the typical work for some field techs. You get in the car, go to the call, hop back in the car, off to the next call, lunch in a drive-thru, sleep in a parking lot. Also, factor in about five to six hours lost to commute.<\/p>\n<p>Imagine a schedule like that for seven+ years and \u00a0when do you have <u>time<\/u> to learn? I believed exactly the same thing about Windows PowerShell until I found myself in a position where I <u>had<\/u> to use it.<\/p>\n<p>I clearly remember the day, too. It was sometime late in 2007. I got a call from a client. \u201cThere\u2019s a vendor application filling up the server with logs. Please find some way to get rid of the old data. Anything over 24 hours old is garbage.\u201d<\/p>\n<p>\u201cOkay,\u201d I thought to myself. \u201cI\u2019m not a developer or coder, but maybe there\u2019s a utility on the Internet, something from Sysinternals. If I have to spend two hours to learn how to write something, I\u2019ll do it.\u201d<\/p>\n<p>Well, I got on the site, and the first thing that I did was hit the local search engines for \u201cErase old files based on date\u201d (or something along those lines). I ran into an article on <em>Hey Scripting Guys<\/em> that showed how to remove files over seven (7) days old from a folder.<\/p>\n<p>It showed this line in PowerShell to get a list of the files ending in .LOG:<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem C:\\Foldername\\*.LOG<\/code><\/p>\n<p>It then showed two additional lines to remove that content. One line grabs the current date, and the other filters the list.<\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem C:\\Foldername\\*.LOG | Where-Object { $_.LastWriteTime \u2013lt $Now.AddDays(7) }<\/code><\/p>\n<p>I didn\u2019t really understand much other than, \u201cThere\u2019s a folder, and this is supposed to show stuff over seven days old. I wonder if I just change that 7 to a 1?\u201d<\/p>\n<p>At that point, I installed PowerShell on a test machine and copied the folder structure that I had to purge. I then made a second copy because the network was slow that day, and I didn\u2019t want to waste time.<\/p>\n<p>I edited the folder name to match the folder that I wanted and the file extension. I also changed that 7 to a 1 and crossed my fingers.<\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem C:\\VendorApp\\*.TXT | Where-Object { $_.LastWriteTime \u2013lt $Now.AddDays(-1) }<\/code><\/p>\n<p>I blinked and looked. It displayed <u>only<\/u> files that were older than today and that had\u00a0the TXT extension. I was floored. To learn how to remove them, I was ready for a long, drawn-out session. \u201cGrumble grumble\u2026I\u2019ll bet VBScript is involved here somewhere.\u201d<\/p>\n<p>To remove the files, I saw only one extra bit of data. Something called <strong>Remove-Item<\/strong> seemed a pretty sensible name. I appended the last bit of code and read a bit further as the article described how to test it without doing anything dangerous by using the <strong>whatif<\/strong> parameter.<\/p>\n<p>I shook my head and muttered something about \u201cPigs flying before this would work,\u201d but I tried it anyway.<\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem C:\\VendorApp\\*.TXT | Where-Object { $_.LastWriteTime \u2013lt $Now.AddDays(-1) } | Remove-Item \u2013whatif<\/code><\/p>\n<p>Staring and blinking at the screen of output, I scrolled up. It looked like something happened to the data. But, upon examining the folder, nothing happened. It really <u>did<\/u> a \u201cWhat if I do this?\u201d<\/p>\n<p>To actually do the damage, it asked me to remove the <strong>\u2013whatif<\/strong> and run the cmdlet.<\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem C:\\VendorApp\\*.TXT | Where-Object { $_.LastWriteTime \u2013lt $Now.AddDays(-1) } | Remove-Item<\/code><\/p>\n<p>\u201cWow!\u201d Staring at the folder, I was speechless. Only 24 hours files left of files ending in TXT.<\/p>\n<p>I had been on site for only 10 minutes, and the problem was solved\u2026.almost.<\/p>\n<p>\u201cI\u2019ll bet this won\u2019t work on the server,\u201d I thought. I was ready for the worst. This was my first time using PowerShell and I was untrusting.<\/p>\n<p>So, repeat process, install PowerShell 1.0 on the Windows Server 2003 R2 environment, copy this script, <u>duplicate<\/u> the folder of data. After all, <em>I<\/em> wasn\u2019t about to see how good their backup system was.<\/p>\n<p>I tested the process, and the results were the same. Now I had to figure out how to schedule this. Oh, this was like DOS. Instead of running cmd.exe or command.com, it was PowerShell.exe.<\/p>\n<p>I made a little folder called \u201cC:\\DataClean\u201d, saved this new script called LogClean.PS1, and set up a scheduled task.<\/p>\n<p style=\"padding-left: 60px\"><code>PowerShell.exe \u2013executionpolicy Bypass \u2013file C:\\DataClean\\LogClean.PS1<\/code><\/p>\n<p>I ran the task and crossed my fingers.<\/p>\n<p>POOF! It all just worked!<\/p>\n<p>I was on site for barely 20 minutes for a two-hour minimum billable call. I was also on the road early and beat rush hour traffic on a Friday.<\/p>\n<p>Hearing AC\/DC\u2019s <em>Highway to Hell<\/em> on the radio and with the knowledge of what technology got me home early that day, I began singing loudly all the way home. Singing to the chorus of the song, the words \u201cI\u2019m using PowerShell! I\u2019m using PowerShell!\u201d kept bursting from\u00a0my lips.<\/p>\n<p>Yes, I love using PowerShell. Over the next while, I\u2019ll try to relate some things that you can use PowerShell for without needing to learn how to script.\u00a0 Even if you, too, are that field tech with no time on your hands.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow.<\/p>\n<p>Until then, always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney<\/strong>\nHonorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Honorary Scripting Guy, Sean Kearney, relates his first experience with PowerShell. I was curious just how difficult it is to use Windows PowerShell. For example, how difficult is it to erase files based on the current date? Honorary Scripting Guy, Sean Kearney, is here today, and I can completely relate to exactly what you\u2019ve [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568,685,641],"tags":[56,154,45],"class_list":["post-79055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Honorary Scripting Guy, Sean Kearney, relates his first experience with PowerShell. I was curious just how difficult it is to use Windows PowerShell. For example, how difficult is it to erase files based on the current date? Honorary Scripting Guy, Sean Kearney, is here today, and I can completely relate to exactly what you\u2019ve [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=79055"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79055\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=79055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=79055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=79055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}