{"id":68,"date":"2012-06-04T12:29:00","date_gmt":"2012-06-04T12:29:00","guid":{"rendered":"\/post\/2012\/06\/04\/Script-to-delete-files-older-thans-x-days.aspx"},"modified":"2012-06-04T12:29:00","modified_gmt":"2012-06-04T12:29:00","slug":"script-to-delete-files-older-thans-x-days","status":"publish","type":"post","link":"https:\/\/www.vatland.no\/index.php\/script-to-delete-files-older-thans-x-days\/","title":{"rendered":"Script to delete files older thans x days"},"content":{"rendered":"<p>This script will delete files older than 10 days.<\/p>\n<p>$Now = Get-Date<br \/>$LastWrite = $Now.AddDays(-10)<br \/>$Files = get-childitem -Path &#8220;C:\\folder_with_files_to_delete&#8221; |Where {$_.CreationTime -le $LastWrite} <br \/>foreach ($File in $Files)<br \/>{Remove-Item -Path $File -Force}<\/p>\n<p>Save this script as &#8220;c:\\scripts\\deleteoldfiles.ps1&#8221;<\/p>\n<p>In schedule task select program\/script to run : &#8220;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe&#8221;<\/p>\n<p>and use : -command &#8220;c:\\scripts\\deleteoldfiles.ps1&#8221; as argument<\/p>\n<p>Remember you has to set the execution policy to allow this script to run. (set-executionpolicy -executionpolicy remotesigned)<\/p>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"color: #ff0000; text-decoration: underline;\">Update:<\/span><\/strong><\/span><\/p>\n<p>For PowerShell 1.0 and filter on file Attribute:<\/p>\n<p># Written by&nbsp; Atle Vatland @ Cegal AS<br \/># Remember to change path,attribute and fileextension.<br \/># When tested OK &#8211; remove&nbsp; WHATIF from Remove-item and the files will be deleted.<br \/># Schedule a task.<br \/># Save this script as &#8220;c:\\scripts\\filename.ps1&#8221;<br \/># In schedule task select program\/script to run : &#8220;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe&#8221;<br \/># and use : -command &#8220;c:\\scripts\\filename.ps1&#8221; as argument<br \/># Remember you has to set the execution policy to allow this script to run. (set-executionpolicy -executionpolicy remotesigned)<br \/>&nbsp;<\/p>\n<p>$Now = Get-Date<br \/>$LastWrite = $Now.AddDays(-14)<br \/>$Files = get-childitem -Path &#8220;E:\\filepath\\*&#8221; -include *.ext |Where {$_.LastWriteTime -le $LastWrite} <br \/>$Today= &#8221; &#8212;&#8212;&#8212;================== Files deleted &#8221; + $Now + &#8220;====================&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8221; <br \/>$Today | Out-File -filepath c:\\scripts\\deletedfiles.log -Append<br \/>foreach ($File in $Files)<\/p>\n<p>{ <br \/>$FI=get-item $File<br \/>&nbsp;if($FI.Attribute -notlike &#8220;*Archive*&#8221;){<br \/>&nbsp; $File | ft -HideTableHeaders | out-file -filepath c:\\scripts\\deletedfiles.log -Append;remove-item $File -whatif<br \/>&nbsp;}<br \/>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script will delete files older than 10 days. $Now = Get-Date$LastWrite = $Now.AddDays(-10)$Files = get-childitem -Path &#8220;C:\\folder_with_files_to_delete&#8221; |Where {$_.CreationTime -le $LastWrite} foreach ($File in $Files){Remove-Item -Path $File -Force} Save this script as &#8220;c:\\scripts\\deleteoldfiles.ps1&#8221; In schedule task select program\/script to run : &#8220;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe&#8221; and use : -command &#8220;c:\\scripts\\deleteoldfiles.ps1&#8221; as argument Remember you has to set &hellip; <a href=\"https:\/\/www.vatland.no\/index.php\/script-to-delete-files-older-thans-x-days\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Script to delete files older thans x days<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-categories"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack-related-posts":[{"id":94,"url":"https:\/\/www.vatland.no\/index.php\/cshtml-files-return-html-error-404-in-blogengine-net-2-5\/","url_meta":{"origin":68,"position":0},"title":"*.cshtml files return html error 404 in BlogEngine.NET 2.5","author":"Atle","date":"August 4, 2011","format":false,"excerpt":"I was searching for some help on this error for days. Tried adding different mappings. Then I found this page : http:\/\/www.sven-s.de\/post\/BlogEngine-HTTP-4043-cshtml-Handler-not-registered-in-Shared-Hosting.aspx \u00a0\" HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":957,"url":"https:\/\/www.vatland.no\/index.php\/hash-tables-in-powershell\/","url_meta":{"origin":68,"position":1},"title":"Hash tables in powershell","author":"Atle","date":"March 23, 2020","format":false,"excerpt":"We all have the need to store data in some kind of arrays. I use hashtables a lot. Preferred use is as a lookup table, I can use 'contains' instead of looping through each item or reference an object by name instead of index number. Lookup table for licenses is\u2026","rel":"","context":"In &quot;CSP&quot;","block_context":{"text":"CSP","link":"https:\/\/www.vatland.no\/index.php\/category\/csp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":950,"url":"https:\/\/www.vatland.no\/index.php\/secretservertycotic-plugin-for-powershell-secretsmanagement-module\/","url_meta":{"origin":68,"position":2},"title":"SecretServer(Thycotic) plugin for powershell SecretsManagement module.","author":"Atle","date":"February 18, 2020","format":false,"excerpt":"With the new powershell module SecretsManagement it is possible to add plugins. The new module is used to get\/add\/remove credentials. I created a plugin for secretserver to this module. To get started you will have to install secretsmanagement: Install-Module -Name Microsoft.Powershell.SecretsManagement -AllowPrerelease If -AllowPrerelase is not an option you would\u2026","rel":"","context":"In &quot;Powershell&quot;","block_context":{"text":"Powershell","link":"https:\/\/www.vatland.no\/index.php\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":934,"url":"https:\/\/www.vatland.no\/index.php\/simple-powershell-mru-list\/","url_meta":{"origin":68,"position":3},"title":"Simple Powershell MRU list","author":"Atle","date":"December 23, 2019","format":false,"excerpt":"When using using my secret server powershell functions I got tired of constantly searching for secret ID's. I had to do a new search just because I could not remember the ID's. So I added some kind of MRU to my get-secretID function. This code block creates to classes mruitem\u2026","rel":"","context":"In &quot;Powershell&quot;","block_context":{"text":"Powershell","link":"https:\/\/www.vatland.no\/index.php\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":100,"url":"https:\/\/www.vatland.no\/index.php\/hyper-v-vm-startup-error-evnet-id-12030\/","url_meta":{"origin":68,"position":4},"title":"Hyper-V VM startup error. Evnet ID 12030","author":"Atle","date":"December 1, 2013","format":false,"excerpt":"Error starting Hyper-V VM : I decieded to delete the VHDX file of a Virtual machine and create a new. The purpose was to do a reinstall, and keep the old file incase I needed som files. After creating the new VHDX file I tried to start the virtual machine\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":84,"url":"https:\/\/www.vatland.no\/index.php\/error-1603-installing-exchange-2010-rollups\/","url_meta":{"origin":68,"position":5},"title":"Error 1603 installing Exchange 2010 RollUp&#8217;s","author":"Atle","date":"October 7, 2011","format":false,"excerpt":"Getting error installing Rollup 5 for Exchange 2010 Sp1. Error 1603. According to msi log file it is ServiceControl.ps1 that is causing the error. Solution was to set execution policy to RemoteSigned and also run the \"ServiceControl.ps1 AfterPatch\" script from the Exchange 2010 Bin folder\u00a0. Also remember to start Powershell\u2026","rel":"","context":"In &quot;Exchange&quot;","block_context":{"text":"Exchange","link":"https:\/\/www.vatland.no\/index.php\/category\/exchange\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":0,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vatland.no\/index.php\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}