Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Deleting Contents of Folder

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-18-2007   #1 (permalink)
Jason Carter
Guest


 

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

My System SpecsSystem Spec
Old 11-18-2007   #2 (permalink)
Keith Hill [MVP]
Guest


 

Re: Deleting Contents of Folder

"Jason Carter" <JasonCarter@xxxxxx> wrote in message
news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@xxxxxx
Quote:

> 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

My System SpecsSystem Spec
Old 11-18-2007   #3 (permalink)
Jason Carter
Guest


 

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:
Quote:

> "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@xxxxxx
Quote:

> > 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
>
My System SpecsSystem Spec
Old 11-18-2007   #4 (permalink)
Jason Carter
Guest


 

Re: Deleting Contents of Folder

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

"Jason Carter" wrote:
Quote:

> 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:
>
Quote:

> > "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> > news:7BA18BD6-34D8-48D0-A604-2C8C3C480D41@xxxxxx
Quote:

> > > 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
> >
My System SpecsSystem Spec
Old 11-19-2007   #5 (permalink)
Keith Hill [MVP]
Guest


 

Re: Deleting Contents of Folder

"Jason Carter" <JasonCarter@xxxxxx> wrote in message
news:C984DEA3-CC62-4758-9592-C66841318909@xxxxxx
Quote:

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

--
Keith

My System SpecsSystem Spec
Old 11-19-2007   #6 (permalink)
Keith Hill [MVP]
Guest


 

Re: Deleting Contents of Folder

"Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@xxxxxx
Quote:

> "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> news:C984DEA3-CC62-4758-9592-C66841318909@xxxxxx
Quote:

>> 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

My System SpecsSystem Spec
Old 11-19-2007   #7 (permalink)
Jason Carter
Guest


 

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:
Quote:

> "Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
> news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@xxxxxx
Quote:

> > "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> > news:C984DEA3-CC62-4758-9592-C66841318909@xxxxxx
Quote:

> >> 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
>
My System SpecsSystem Spec
Old 11-19-2007   #8 (permalink)
Jason Carter
Guest


 

Re: Deleting Contents of Folder

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

"Keith Hill [MVP]" wrote:
Quote:

> "Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
> news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@xxxxxx
Quote:

> > "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> > news:C984DEA3-CC62-4758-9592-C66841318909@xxxxxx
Quote:

> >> 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
>
My System SpecsSystem Spec
Old 11-19-2007   #9 (permalink)
JSC
Guest


 

Re: Deleting Contents of Folder

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?

"Jason Carter" wrote:
Quote:

> I am going to try to modify it to delete subfolders too.
>
> "Keith Hill [MVP]" wrote:
>
Quote:

> > "Keith Hill [MVP]" <r_keith_hill@xxxxxx_spam_I> wrote in message
> > news:B81EBA3C-33AB-47E1-BBC3-C17EF2F3394B@xxxxxx
Quote:

> > > "Jason Carter" <JasonCarter@xxxxxx> wrote in message
> > > news:C984DEA3-CC62-4758-9592-C66841318909@xxxxxx
> > >> 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
> >
My System SpecsSystem Spec
Old 11-19-2007   #10 (permalink)
Keith Hill [MVP]
Guest


 

Re: Deleting Contents of Folder

"JSC" <JSC@xxxxxx> wrote in message
news:B71B370C-D031-4DFF-B0A0-F6784A755286@xxxxxx
Quote:

> 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

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
moving folder contents to a new folder A Live Folder Share 1 05-25-2008 03:56 AM
Deleting System Volume Information contents in Vista User66 Vista installation & setup 4 05-20-2008 01:49 PM
Deleting SpamKiller Contents rdrunners2 Vista mail 4 11-01-2007 06:16 PM
Temp dir - deleting contents on shutting down Dave Horne Vista General 13 05-02-2007 07:31 AM
Access folder contents Scott Vista General 3 02-17-2007 06:37 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51