Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - deleting files using a date criteria

Reply
 
Old 10-22-2007   #1 (permalink)
Walid


 
 

deleting files using a date criteria

Hello:

how do I delete all files that are 6 months older in a directory of a local
or remote server using powershell script? Any help would be appreciated!

Thanks!

WG

My System SpecsSystem Spec
Old 10-22-2007   #2 (permalink)
Brandon Shell [MVP]


 
 

Re: deleting files using a date criteria

get-childitem $RemoteShare | where{!$_.PSisContainer -and $_.LastWriteTime
-gt (get-date).addmonths(-6)}


Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

W> Hello:
W>
W> how do I delete all files that are 6 months older in a directory of a
W> local or remote server using powershell script? Any help would be
W> appreciated!
W>
W> Thanks!
W>
W> WG
W>


My System SpecsSystem Spec
Old 10-22-2007   #3 (permalink)
Brandon Shell [MVP]


 
 

Re: deleting files using a date criteria

oh.. add -recurse if you want to recurse subfolders as well.

get-childitem $RemoteShare -recurse ...

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

BS> get-childitem $RemoteShare | where{!$_.PSisContainer -and
BS> $_.LastWriteTime -gt (get-date).addmonths(-6)}
BS>
BS> Brandon Shell
BS> ---------------
BS> Blog: http://www.bsonposh.com/
BS> PSH Scripts Project: www.codeplex.com/psobject
W>> Hello:
W>>
W>> how do I delete all files that are 6 months older in a directory of
W>> a local or remote server using powershell script? Any help would be
W>> appreciated!
W>>
W>> Thanks!
W>>
W>> WG
W>>


My System SpecsSystem Spec
Old 10-22-2007   #4 (permalink)
Oisin Grehan


 
 

Re: deleting files using a date criteria

On Oct 22, 2:26 pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
wrote:
Quote:

> get-childitem $RemoteShare | where{!$_.PSisContainer -and $_.LastWriteTime
> -gt (get-date).addmonths(-6)}
>
> Brandon Shell
> ---------------
> Blog:http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
>
> W> Hello:
> W>
> W> how do I delete all files that are 6 months older in a directory of a
> W> local or remote server using powershell script? Any help would be
> W> appreciated!
> W>
> W> Thanks!
> W>
> W> WG
> W>
Eeek! surely you mean -LT (get-date).addmonths(-6) (not -GT) ?

your current code gets all items *modified in the last 6 months*, not
files older than 6 months.

- Oisin

My System SpecsSystem Spec
Old 10-22-2007   #5 (permalink)
Brandon Shell [MVP]


 
 

Re: deleting files using a date criteria

Details... Details...

Thanks for the catch Oisin, This is exactly why I do not ever add the remove-item
at the end

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

OG> On Oct 22, 2:26 pm, Brandon Shell [MVP] <a_bshell.m...@xxxxxx>
OG> wrote:
OG>
Quote:
Quote:

>> get-childitem $RemoteShare | where{!$_.PSisContainer -and
>> $_.LastWriteTime -gt (get-date).addmonths(-6)}
>>
>> Brandon Shell
>> ---------------
>> Blog:http://www.bsonposh.com/
>> PSH Scripts Project: www.codeplex.com/psobject
>> W> Hello:
>> W>
>> W> how do I delete all files that are 6 months older in a directory
>> of a
>> W> local or remote server using powershell script? Any help would be
>> W> appreciated!
>> W>
>> W> Thanks!
>> W>
>> W> WG
>> W>
OG> Eeek! surely you mean -LT (get-date).addmonths(-6) (not -GT) ?
OG>
OG> your current code gets all items *modified in the last 6 months*,
OG> not files older than 6 months.
OG>
OG> - Oisin
OG>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Date Type mismatch in criteria expression VB Script
Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed? PowerShell
deleting files that meet specific criteria PowerShell
WMP deleting metadata by itself: album, genre, date... Vista music pictures video
last access date for files PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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