Delete Files where-object CreatedDate

Hi Guys i have the following script.

This is meant to get all files within a directory and delete it if it older 
than 31 days can someone assist me as this is currently not working as this 
script deletes based on LastWriteTime and Not Create Date.

Can someone tell me what I’m doing wrong is this script below, or perhaps 
provide something that works

All i need is a script that checks a specific directory that I supply, for 
example

D:\FTP\Public\

And Deletes all Files and Folders Older (Create Date) than 31 days
#Date Created 28/01/2010
#Owner Marcus Van Wyk vanwykm@gess.co.za 0824140893
#Sets variable Date to current date
$Date=get-date


#Sets Date variable back 31 days
$Date=$Date.AddDays(-31)
#Set Datefn variable with correct file name conven
$Datefn=Get-Date -format M.d.yyyy
#Set The File Name for the log file
$DelFileName = $Datefn
#Set The File Ext for the log file
$DelFileExt = ".log"
#Set The File Name With Ext for the log file
$DelFileName = $DelFileName + $DelFileExt
#Set Log Path
$LogPath = "C:\Users\vanwykm\Documents\PS Scripts\"
#DEBUG OPTIONS
#Write-Host $DelFileName
#Write-Host $Date
#Write-Host $Datefn

#Checks Path For Old File and Logs it.
Get-ChildItem 'C:\Users\vanwykm\Documents\Email Attachments\Francois 
Grobler' -recurse | where-object {$_.CreatedDate -lt $date} | Out-File 
"$LogPath\$DelFileName"

#Deletes Old Files.
Get-ChildItem 'C:\Users\vanwykm\Documents\Email Attachments\Francois 
Grobler' -recurse | where-object {$_.CreatedDate -lt $date} | remove-item 
-recurse -force

0
Utf
2/2/2010 2:35:01 PM
windows.powershell 690 articles. 0 followers. Follow

1 Replies
1578 Views

Similar Articles

[PageSpeed] 28

There is no CreatedDate property returned on a file from get-childitem.  You 
should be using CreationTime I believe.

"vanwykm" wrote:

> Hi Guys i have the following script.
> 
> This is meant to get all files within a directory and delete it if it older 
> than 31 days can someone assist me as this is currently not working as this 
> script deletes based on LastWriteTime and Not Create Date.
> 
> Can someone tell me what I’m doing wrong is this script below, or perhaps 
> provide something that works
> 
> All i need is a script that checks a specific directory that I supply, for 
> example
> 
> D:\FTP\Public\
> 
> And Deletes all Files and Folders Older (Create Date) than 31 days
> #Date Created 28/01/2010
> #Owner Marcus Van Wyk vanwykm@gess.co.za 0824140893
> #Sets variable Date to current date
> $Date=get-date
> 
> 
> #Sets Date variable back 31 days
> $Date=$Date.AddDays(-31)
> #Set Datefn variable with correct file name conven
> $Datefn=Get-Date -format M.d.yyyy
> #Set The File Name for the log file
> $DelFileName = $Datefn
> #Set The File Ext for the log file
> $DelFileExt = ".log"
> #Set The File Name With Ext for the log file
> $DelFileName = $DelFileName + $DelFileExt
> #Set Log Path
> $LogPath = "C:\Users\vanwykm\Documents\PS Scripts\"
> #DEBUG OPTIONS
> #Write-Host $DelFileName
> #Write-Host $Date
> #Write-Host $Datefn
> 
> #Checks Path For Old File and Logs it.
> Get-ChildItem 'C:\Users\vanwykm\Documents\Email Attachments\Francois 
> Grobler' -recurse | where-object {$_.CreatedDate -lt $date} | Out-File 
> "$LogPath\$DelFileName"
> 
> #Deletes Old Files.
> Get-ChildItem 'C:\Users\vanwykm\Documents\Email Attachments\Francois 
> Grobler' -recurse | where-object {$_.CreatedDate -lt $date} | remove-item 
> -recurse -force
> 
0
Utf
2/2/2010 3:03:02 PM
Reply:

Similar Artilces:

Can I delete my switchboard and make a new one?
I do not like my switchboard and would like to delete this one and start all over. But when I did delete it I couldn't make another one, because I couldn't delete the default. What should I do?Thanks,Dustin Just delete the form and the table that were created by the wizard, then start over.-- Rick B"Dustin" <Dustin@discussions.microsoft.com> wrote in message news:42F94A44-9352-4D02-9104-65593869B271@microsoft.com...>I do not like my switchboard and would like to delete this one and start >all> over. But when I did delete it I couldn't make another one, beca...

New mail appearing as old deleted item
When new mail arrives in inbox an old long deleted item appears in place of the new mail showing in bold as new mail. The real new mail is not visible at all and is therefore lost. This problem also used to happen in Windows Mail so I bought Outlook 2007 but it still happens intermittantly. -- Kate What do you see in your Unread Mail folder? "Kate B" <KateB@discussions.microsoft.com> wrote in message news:D9A95DD7-CFC4-423F-9B2B-F8B72177E2CE@microsoft.com... > When new mail arrives in inbox an old long deleted item appears in place of > the new mail...

Converting XLS file to QIF or to OFX
How do I safely and securely convert an excel file (xls) to a QIF or OFX file? "dreamchaser" wrote: > How do I safely and securely convert an excel file (xls) to a QIF or OFX file? In Excel, save the file to CSV and ustilise iCreateOFX Basic from: http://icreateofx.co.uk/Convert-CSV-to-OFX to convert the saved CSV file to OFX. ...

Inserting object
Is there any macro that will allow the use of the insert picture from scanner? I have tried to use the recorder but it never recorded anything when I used it. Any help would be great Thanks Greg hiiiiiii dear friend if u can insert picture from menu for Exp: insert menu Picture ---> from Scanner or Camera if u have such useful command available so whyy u want to try macro. after u try from insert menu it may work out for u but first install the scanner good luck !!!!!!! "Greg B" wrote: > Is there any macro that will allow the use of the insert picture from > sc...

conversion lotus 123 files to excel -- problem
I am converting lotus123 files to excel2002. One problem is that in lotus, literals are ignored when found in a cell within a formula. Excel on the other hand is not doing this and therefore causing #value errors on all the formulas where this occurs. Is there a way to handle this in excel other than manually having to change all the formulas? ...

cannot view objects
Since 01/07/2005 I can't view objects in MS Publisher 2003. I could prior to that. I haven't done any OS updates. Help! Have you tried View, pictures, detailed display... -- Mary Sauer MS MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Jay in Colorado" <Jay in Colorado@discussions.microsoft.com> wrote in message news:30D92C79-48ED-4CF3-8FF6-51AC155C86ED@microsoft.com... > Since 01/07/2005 I can't view objects in MS Publisher 2003. I could prior to > that. I haven't done any OS updates. Help! Yes, thanks but it...

Deleting record from bindingsource
Hi I have a winform app with form control bound to a binding source TblClientsBindingSource. The binding source has a data table as its data source as per below code; dAdapter = New OleDbDataAdapter("SELECT * FROM tblClients WHERE ...", Conn) dAdapter.Fill(dTable) TblClientsBindingSource.DataSource = dTable My question is; how do I delete the record currently pointed to by the binding source from the backend database table tblClients? Thanks Regards John wrote: > Hi > > I have a winform app with form control bound to a binding source &g...

How do I get excel files to open automatically from directories?
When I try to open excel files from the directory or from desktop I only get a blank worksheet not the file. I have to then go through File Open to get the file I want. There must be a way to have them open automatically. On Mon, 2 Jan 2006 21:22:01 -0800, Damian <Damian@discussions.microsoft.com> wrote: >When I try to open excel files from the directory or from desktop I only get >a blank worksheet not the file. I have to then go through File Open to get >the file I want. > >There must be a way to have them open automatically. Go to Tools -> Options -> Gen...

Odd problem with worksheets when opening file
Okay, here's the odd problem that's come up. When you double click on a excel file, excel opens up, but you can onl see the toolbars. The grid area looks like a snapshot of whatever you current background is before the file opened up. If you were showin your desktop, after the excel file opened, you'd still see your deskto in the datagrid area. If you start up a new excel file, then do the File>Open>file name, th file will open correctly. This problem happens across users here. Everyone is currently runnin office 2K. Any ideas on what causes the problem and any...

Outlook 2002 multiple file opening
In Outlook 2000, CTRL A would select all attached picture files, then Open would, in my case, open the files in Paintshop Pro. With the 2002 version I cannot find a way to do this. Any suggestions? ...

"File: send to mail recipient" not working
When users (on WinXP) select file, "send to mail recipient", no response from Outlook 2002. This occurs either when Outlook is open or closed. Problem happens when rt. clicking on .doc, .xls, .pdf's....all files. Any ideas?? Thanks!! ...

can't change/delete bills and deposits
suddenly one day i realized that when i tried to change a bill, i could click "ok" but nothing would happen. I thought it was because i upgraded to 2004, so i deleted the bills and rescheduled. about 5 of the bills will not delete either. the error message is: "Money could not write to your Money file. The operation could not be performed or another application finished this task before you. Please try again." Now some of the newly scheduled bills will not allow me to change the properties, i click ok but nothing happens when i edit the series. I have repaired ...

Deleting Folders
Hi, I have all my accounts synchronised. I have inadvertently deleted a folder with many sub-folders under my local folders. Does anyone know if I can retrieve and how? Appreciate any help. This info. is very important. In case of Exchange; Tools-> Recover Deleted Items... In case of a pst; http://www.howto-outlook.com/howto/restoredeleteditemsfromanoutlookpst.htm -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -Backup and Restore -Create an Office XP CD slipstreamed with Service Pack 3 ----- "Jac" <anonymous@discussions.microsoft.com> wr...

"Object variable or With block not set."
-- RiverCrescent On Sun, 14 Mar 2010 16:22:01 -0700, CedarPoint <CedarPoint@discussions.microsoft.com> wrote: You are referencing a variable incorrectly or misspelling it. If you would like help fixing your code... post your code. We can't see it from here. -- John W. Vinson [MVP] ...

odd files created
Every time I open and edit an excel spreadsheet on a network share, small odd files get created. They are usually no larger than 25-40k and don't have any extensions to them. Looking at the properties page for any file, the file description says File. Anyone know what this is from or how to get rid of them? Permissions are setup correctly for me, Word files don't have this behavior. Excel 2000 SP3 Thanks, Hi Brian, A file the same size as the workbook would be created in the same directory as the workbook. The filename would be nonsensical (or appear to be random) character...

Excel Template Wizard, Very Large File Size
Background: Excel 2000. I created a spreadsheet and used the template wizard that links to an Access 2000 database. The template was later used to create one record and saved as a spreadsheet. The database also contains this one record. Spreadsheet and database work fine. Later I reduced the Access field size properties (none are larger than 150, most are less than 50). There are 35 fields. Unexpected result: The Excel spreadsheet (template) and the linked Access database are huge. The spreadsheet is 35MB and the associated Access 2000 database is 24MB. Each contains one record (a si...

"Delete it from Server" Not Deleting From Server
Hello...and thank you for whatever assistance you might be able to render. I have set a message rule, in OE, which calls for certain messages to be deleted from the server. If I understand this part of the rule correctly, this means the message will never be downloaded...and that it will be completely deleted at the server level, so that it isn't counting against my server disk storage level, either. Figuring I'm correct in that presumption--and please let me know if I'm wrong--I seem to have some messages making it through that should absolutely be deleted at th...

Date & Time file was last saved
Is there away to display in a cell the date & time the file was last saved? I know you can do this in word but is it possible in Excel 2002? Here is an UDF Function DocProps(prop As String) application.volatile On Error GoTo err_value DocProps = ActiveWorkbook.BuiltinDocumentProperties(prop) Exit Function err_value: DocProps = CVErr(xlErrValue) End Function Now you can use the following formula in one of your cells: =DocProps("Last save time") Obviously, the file has to have been saved. -- HTH Bob Phillips ... looking out across Poole Harbour to ...

OL2000
When opening an attachment in OL2000, the program creates a file in a temporary directory on my hard drive under C:\Documents and Settings\User\Local Settings\Temporary Internet Files\OLK6A2\. This directory is filling up and I would like to clear it. Unfortunately in XP SP2, this directly is not visible in when exploring files in my computer. I believe that I have all "show hidden files and folders" settings checked. Also, the file explorer shows (sometimes) that there is a sub directory under "temporary internet files," but it does not show up when I look further....

With TierDeveloper you can Design, Generate and Test Data Objects in no time
AlachiSoft �TierDeveloper� eradicates the problem of writing thousands of lines of code for your middle tier. Many software solutions on the market can do it for you but according to our research there are few solutions in market, which can really be of any benefit to the software developer, And TierDeveloper is one of them. TierDeveloper 3.0. is an O/R mapping and code generation tool that helps software developers boost their development efforts and generate n-tier applications in less time. With its easy to use and flexible interface, TierDeveloper lets you map middle-tier data ob...

Is there a way to automatically delete duplicate entries in Excel.
Is there a way to automatically delete duplicate entries in Excel? If there are Invoice #'s or some other numerical value, can they be deleted or selected without having to search through the document or filter? Hi use 'Data - filter - Advanced Filter' and check 'Unique entries' -- Regards Frank Kabel Frankfurt, Germany NK wrote: > Is there a way to automatically delete duplicate entries in Excel? > If there are Invoice #'s or some other numerical value, can they be > deleted or selected without having to search through the document or > filter? N...

Corrupt Access 2003 File
I was working on this file earlier today, I even just opened it and it was fine and all of a sudden there is a write delay to the save the file, I closed it and I try opening it again and it is corrupted. I don't get it. The message I am getting is below. What does that mean. I just finished making a lost of update to the file. The last time I backed it up was Tuesday, and I have done a lot of work to it between then and now. I don't have the time to go back and redo all that work again. Any help will be appreciated. "Microsoft Office Access has detected corruption in this ...

Visio Professional 7
i have several visio files (20-40) pages in each and i need to merge them into a single large visio file... is there a way to do this... inserting a page at a time and copying the drawing is not practical On Wed, 30 Apr 2008 07:38:01 -0700, sunman <sunman@discussions.microsoft.com> wrote: >i have several visio files (20-40) pages in each and i need to merge them >into a single large visio file... is there a way to do this... inserting a >page at a time and copying the drawing is not practical I've just added this feature to the utilities at http://www.visio-utilities....

protect from deleting
Hi all, I would like to know, how can i prevent a work book from other to deleting without my knowledge. i mean its just like a folder. thanks --- Message posted from http://www.ExcelForum.com/ A workbook is just a file. Change its NTFS permissions. "nowfal >" <<nowfal.17cdkl@excelforum-nospam.com> wrote in message news:nowfal.17cdkl@excelforum-nospam.com... > Hi all, > I would like to know, how can i prevent a work book from other > to deleting without my knowledge. i mean its just like a folder. ...

What OL2003 does after you clear many messages from Deleted Items
I find this annoying and am wondering if anyone else has noticed it. I use OL2003 with a PST (no Exchange) on XPSP2. My PST is usually not much over 100MB. When going into Deleted Items and clearing out, I don't know, on the order of at least 500 or 1000 messages, I notice a sudden amount of disk activity. I've used Sysinternal's Filemon to find that it's Outlook, presumably doing some kind of subtle behind-the-scenes compaction. (Note that I have AutoArchive disabled, so it's not that.) Problem is that this stealth compaction goes on for 10, 15, 20 minutes or more,...