TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Deleting Contents of Folder

Microsoft Windows Powershell

 
 
Thread Tools Display Modes
Unread 20-11-2007, 07:25 AM   #1
=?Utf-8?B?SmFzb24gQ2FydGVy?=
Guest
 
Posts: n/a
Deleting Contents of Folder

Would anybody give me some direction on generating a script that would scroll
through a drive on a server and delete the contents of a users folder (leave
the folder there) if the folder hasn't been accessed in a certain ammount of
time?

Location example:

d:\users\USERNAME
 
Unread 20-11-2007, 07:25 AM   #2
Keith Hill [MVP]
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

"Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@microsoft.com...
> Would anybody give me some direction on generating a script that would
> scroll
> through a drive on a server and delete the contents of a users folder
> (leave
> the folder there) if the folder hasn't been accessed in a certain ammount
> of
> time?
>
> Location example:
>
> d:\users\USERNAME


It depends on what you mean by "accessed" but if you mean when the user last
wrote anything to the folder then you can use something like this:

$UserDir = d:\users\<username>
$DateLimit = (get-date).AddDays(-30)
$newItems = Get-ChildItem $UserDir\* -rec | Where {$_.LastWriteTime -gt
$DateLimit}
if (!$newItems) {
remove-item $UserDir\* -r -whatif
}

Remove the -whatif to actually do the delete.

--
Keith

 
Unread 20-11-2007, 07:25 AM   #3
=?Utf-8?B?SmFzb24gQ2FydGVy?=
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

Thats what I meant. Thanks a lot. Is there anyway you can do this without
specifying the username? Just process the whole Users directory with this
script for each folder?

"Keith Hill [MVP]" wrote:

> "Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
> news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@microsoft.com...
> > Would anybody give me some direction on generating a script that would
> > scroll
> > through a drive on a server and delete the contents of a users folder
> > (leave
> > the folder there) if the folder hasn't been accessed in a certain ammount
> > of
> > time?
> >
> > Location example:
> >
> > d:\users\USERNAME

>
> It depends on what you mean by "accessed" but if you mean when the user last
> wrote anything to the folder then you can use something like this:
>
> $UserDir = d:\users\<username>
> $DateLimit = (get-date).AddDays(-30)
> $newItems = Get-ChildItem $UserDir\* -rec | Where {$_.LastWriteTime -gt
> $DateLimit}
> if (!$newItems) {
> remove-item $UserDir\* -r -whatif
> }
>
> Remove the -whatif to actually do the delete.
>
> --
> Keith
>

 
Unread 20-11-2007, 07:25 AM   #4
=?Utf-8?B?SmFzb24gQ2FydGVy?=
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

Using the * will do this correct: d:\users\* ?

"Jason Carter" wrote:

> Thats what I meant. Thanks a lot. Is there anyway you can do this without
> specifying the username? Just process the whole Users directory with this
> script for each folder?
>
> "Keith Hill [MVP]" wrote:
>
> > "Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
> > news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@microsoft.com...
> > > Would anybody give me some direction on generating a script that would
> > > scroll
> > > through a drive on a server and delete the contents of a users folder
> > > (leave
> > > the folder there) if the folder hasn't been accessed in a certain ammount
> > > of
> > > time?
> > >
> > > Location example:
> > >
> > > d:\users\USERNAME

> >
> > It depends on what you mean by "accessed" but if you mean when the user last
> > wrote anything to the folder then you can use something like this:
> >
> > $UserDir = d:\users\<username>
> > $DateLimit = (get-date).AddDays(-30)
> > $newItems = Get-ChildItem $UserDir\* -rec | Where {$_.LastWriteTime -gt
> > $DateLimit}
> > if (!$newItems) {
> > remove-item $UserDir\* -r -whatif
> > }
> >
> > Remove the -whatif to actually do the delete.
> >
> > --
> > Keith
> >

 
Unread 20-11-2007, 07:25 AM   #5
Keith Hill [MVP]
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

"Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
news:C984DEA3-CC62-4758-9592-C66841318909@microsoft.com...
> Using the * will do this correct: d:\users\* ?
>


Yep.

--
Keith

 
Unread 20-11-2007, 07:25 AM   #6
Keith Hill [MVP]
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.no_spam_I> wrote in message
news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@microsoft.com...
> "Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
> news:C984DEA3-CC62-4758-9592-C66841318909@microsoft.com...
>> Using the * will do this correct: d:\users\* ?
>>

>
> Yep.


Oops. You didn't want to delete the user's folder so try this instead:

$DateLimit = (get-date).AddDays(-30)
foreach ($UserDir in (get-childitem d:\users | where {$_.PSIsContainer})) {
$newItems = Get-ChildItem $UserDir -rec | Where {$_.LastWriteTime -gt
$DateLimit}
if (!$newItems) {
remove-item $UserDir\* -r -whatif
}
}

--
Keith

 
Unread 20-11-2007, 07:25 AM   #7
Keith Hill [MVP]
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

"JSC" <JSC@discussions.microsoft.com> wrote in message
news:B71B370C-D031-4DFF-B0A0-F6784A755286@microsoft.com...
> In testing, what I am seeing here is that all the sub files are getting
> deleted, which are making the sub folders last write time change so the
> actually subfolders are not getting deleted if they are present. Would I
> add
> a remove-item $UserDir\*\* -force at the end?


The -r or -recurse should take of recursively deleting. You might want to
use -force just to take care of any read-only files.

--
Keith

 
Unread 20-11-2007, 07:25 AM   #8
=?Utf-8?B?SmFzb24gQ2FydGVy?=
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

Alright thanks. And now I am starting to understand this stuff by looking at
some code.

"Keith Hill [MVP]" wrote:

> "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.no_spam_I> wrote in message
> news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@microsoft.com...
> > "Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
> > news:C984DEA3-CC62-4758-9592-C66841318909@microsoft.com...
> >> Using the * will do this correct: d:\users\* ?
> >>

> >
> > Yep.

>
> Oops. You didn't want to delete the user's folder so try this instead:
>
> $DateLimit = (get-date).AddDays(-30)
> foreach ($UserDir in (get-childitem d:\users | where {$_.PSIsContainer})) {
> $newItems = Get-ChildItem $UserDir -rec | Where {$_.LastWriteTime -gt
> $DateLimit}
> if (!$newItems) {
> remove-item $UserDir\* -r -whatif
> }
> }
>
> --
> Keith
>

 
Unread 20-11-2007, 07:25 AM   #9
Keith Hill [MVP]
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

"JSC" <JSC@discussions.microsoft.com> wrote in message
news:B71B370C-D031-4DFF-B0A0-F6784A755286@microsoft.com...
> In testing, what I am seeing here is that all the sub files are getting
> deleted, which are making the sub folders last write time change so the
> actually subfolders are not getting deleted if they are present. Would I
> add
> a remove-item $UserDir\*\* -force at the end?


The -r or -recurse should take of recursively deleting. You might want to
use -force just to take care of any read-only files.

--
Keith

 
Unread 20-11-2007, 07:25 AM   #10
=?Utf-8?B?SmFzb24gQ2FydGVy?=
Guest
 
Posts: n/a
Re: Deleting Contents of Folder

I am going to try to modify it to delete subfolders too.

"Keith Hill [MVP]" wrote:

> "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.no_spam_I> wrote in message
> news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@microsoft.com...
> > "Jason Carter" <JasonCarter@discussions.microsoft.com> wrote in message
> > news:C984DEA3-CC62-4758-9592-C66841318909@microsoft.com...
> >> Using the * will do this correct: d:\users\* ?
> >>

> >
> > Yep.

>
> Oops. You didn't want to delete the user's folder so try this instead:
>
> $DateLimit = (get-date).AddDays(-30)
> foreach ($UserDir in (get-childitem d:\users | where {$_.PSIsContainer})) {
> $newItems = Get-ChildItem $UserDir -rec | Where {$_.LastWriteTime -gt
> $DateLimit}
> if (!$newItems) {
> remove-item $UserDir\* -r -whatif
> }
> }
>
> --
> Keith
>

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 08:13 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional