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 Users Folder - Script Help

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-10-2008   #1 (permalink)
JSC
Guest


 

Deleting Users Folder - Script Help

I have a script that goes out and deletes the contents of users network
shares if they have not accessed them in 365 days. It does leave the folder
itself in case this user deceides to start using it again. Instead of going
by the last time the folder was wrote to, I want to revise the script to read
in from an input file with a list of users I will query out of Active
Directory. This should decrease the processing time of tyring to get when
the folder was last wrote to.

Would it be better to export that list to a text file and use the
get-content cmdlet?

Or export them to a .CSV file and use the import-csv cmdlet?

Either way I guess would work, but I was trying to figure out how to get it
to loop so it processes the whole file.

The current script is below:

# Change location to the Users directory
set-location E:\Users

#Set the numbers of days back we want to purge
$DateLimit = (get-date).AddDays(-365)

#Check last time the folders were wrote to
foreach ($UserDir in (get-childitem X:\Users | where {$_.PSIsContainer})) {
$newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime -gt
$DateLimit}

#Remove files and subfolders including read only files
if (!$newItems) {
remove-item $UserDir\* -recurse -force
}
}

My System SpecsSystem Spec
Old 01-10-2008   #2 (permalink)
Shay Levi
Guest


 

Re: Deleting Users Folder - Script Help

If you have more than one value per user you should use csv.


$DateLimit = (get-date).AddDays(-365)

#Check last time the folders were wrote to
get-content users.txt | foreach {
$UserDir = "X:\Users\$_"
if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
Get-ChildItem $UserDir -rec | remove-item -force
}
}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I have a script that goes out and deletes the contents of users
> network shares if they have not accessed them in 365 days. It does
> leave the folder itself in case this user deceides to start using it
> again. Instead of going by the last time the folder was wrote to, I
> want to revise the script to read in from an input file with a list of
> users I will query out of Active Directory. This should decrease the
> processing time of tyring to get when the folder was last wrote to.
>
> Would it be better to export that list to a text file and use the
> get-content cmdlet?
>
> Or export them to a .CSV file and use the import-csv cmdlet?
>
> Either way I guess would work, but I was trying to figure out how to
> get it to loop so it processes the whole file.
>
> The current script is below:
>
> # Change location to the Users directory
> set-location E:\Users
> #Set the numbers of days back we want to purge
> $DateLimit = (get-date).AddDays(-365)
> #Check last time the folders were wrote to
> foreach ($UserDir in (get-childitem X:\Users | where
> {$_.PSIsContainer})) {
> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
> -gt
> $DateLimit}
> #Remove files and subfolders including read only files
> if (!$newItems) {
> remove-item $UserDir\* -recurse -force
> }
> }

My System SpecsSystem Spec
Old 01-10-2008   #3 (permalink)
JSC
Guest


 

Re: Deleting Users Folder - Script Help

I don't have one than one value per user.

I just want to read in the list of users who need this procedure done from a
file.

The the whole section where you check the last time the folder was wrote to
can be removed because I can query AD the last time the user logged on to see
if they are still active. Guess I didn't explain it very well.

I am going to get a list from AD of users who haven't logged into the
network in the past 365 days, export those to a list, and read in from that
list in my script to delete the contents of the users folders who appear in
the list, but leave the actual user folder itself.

"Shay Levi" wrote:
Quote:

> If you have more than one value per user you should use csv.
>
>
> $DateLimit = (get-date).AddDays(-365)
>
> #Check last time the folders were wrote to
> get-content users.txt | foreach {
> $UserDir = "X:\Users\$_"
> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
> Get-ChildItem $UserDir -rec | remove-item -force
> }
> }
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > I have a script that goes out and deletes the contents of users
> > network shares if they have not accessed them in 365 days. It does
> > leave the folder itself in case this user deceides to start using it
> > again. Instead of going by the last time the folder was wrote to, I
> > want to revise the script to read in from an input file with a list of
> > users I will query out of Active Directory. This should decrease the
> > processing time of tyring to get when the folder was last wrote to.
> >
> > Would it be better to export that list to a text file and use the
> > get-content cmdlet?
> >
> > Or export them to a .CSV file and use the import-csv cmdlet?
> >
> > Either way I guess would work, but I was trying to figure out how to
> > get it to loop so it processes the whole file.
> >
> > The current script is below:
> >
> > # Change location to the Users directory
> > set-location E:\Users
> > #Set the numbers of days back we want to purge
> > $DateLimit = (get-date).AddDays(-365)
> > #Check last time the folders were wrote to
> > foreach ($UserDir in (get-childitem X:\Users | where
> > {$_.PSIsContainer})) {
> > $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
> > -gt
> > $DateLimit}
> > #Remove files and subfolders including read only files
> > if (!$newItems) {
> > remove-item $UserDir\* -recurse -force
> > }
> > }
>
>
>
My System SpecsSystem Spec
Old 01-10-2008   #4 (permalink)
Shay Levi
Guest


 

Re: Deleting Users Folder - Script Help


Export users to csv or query them directly from AD and then pipe to foreach.

If you export manually to csv:

import-csv users.csv | foreach {
# check if the folder exists
if(test-path "$UserDir\$user" -pathtype container){
Get-ChildItem "$UserDir\$user" -recurse | remove-item -force
} else {
write-warning "$UserDir\$user doesn't exist"
}
}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I don't have one than one value per user.
>
> I just want to read in the list of users who need this procedure done
> from a file.
>
> The the whole section where you check the last time the folder was
> wrote to can be removed because I can query AD the last time the user
> logged on to see if they are still active. Guess I didn't explain it
> very well.
>
> I am going to get a list from AD of users who haven't logged into the
> network in the past 365 days, export those to a list, and read in from
> that list in my script to delete the contents of the users folders who
> appear in the list, but leave the actual user folder itself.
>
> "Shay Levi" wrote:
>
Quote:

>> If you have more than one value per user you should use csv.
>>
>> $DateLimit = (get-date).AddDays(-365)
>>
>> #Check last time the folders were wrote to
>> get-content users.txt | foreach {
>> $UserDir = "X:\Users\$_"
>> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
>> Get-ChildItem $UserDir -rec | remove-item -force
>> }
>> }
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> I have a script that goes out and deletes the contents of users
>>> network shares if they have not accessed them in 365 days. It does
>>> leave the folder itself in case this user deceides to start using it
>>> again. Instead of going by the last time the folder was wrote to, I
>>> want to revise the script to read in from an input file with a list
>>> of users I will query out of Active Directory. This should decrease
>>> the processing time of tyring to get when the folder was last wrote
>>> to.
>>>
>>> Would it be better to export that list to a text file and use the
>>> get-content cmdlet?
>>>
>>> Or export them to a .CSV file and use the import-csv cmdlet?
>>>
>>> Either way I guess would work, but I was trying to figure out how to
>>> get it to loop so it processes the whole file.
>>>
>>> The current script is below:
>>>
>>> # Change location to the Users directory
>>> set-location E:\Users
>>> #Set the numbers of days back we want to purge
>>> $DateLimit = (get-date).AddDays(-365)
>>> #Check last time the folders were wrote to
>>> foreach ($UserDir in (get-childitem X:\Users | where
>>> {$_.PSIsContainer})) {
>>> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
>>> -gt
>>> $DateLimit}
>>> #Remove files and subfolders including read only files
>>> if (!$newItems) {
>>> remove-item $UserDir\* -recurse -force
>>> }
>>> }

My System SpecsSystem Spec
Old 01-10-2008   #5 (permalink)
Shay Levi
Guest


 

Re: Deleting Users Folder - Script Help


Add the -WhatIf parameter, it allows you to find what would have happen if
you actually executed it.

Get-ChildItem "$UserDir\$user" -recurse | remove-item -force -WhatIf

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Export users to csv or query them directly from AD and then pipe to
> foreach.
>
> If you export manually to csv:
>
> import-csv users.csv | foreach {
> # check if the folder exists
> if(test-path "$UserDir\$user" -pathtype container){
> Get-ChildItem "$UserDir\$user" -recurse | remove-item -force
> } else {
> write-warning "$UserDir\$user doesn't exist"
> }
> }
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>> I don't have one than one value per user.
>>
>> I just want to read in the list of users who need this procedure done
>> from a file.
>>
>> The the whole section where you check the last time the folder was
>> wrote to can be removed because I can query AD the last time the user
>> logged on to see if they are still active. Guess I didn't explain
>> it very well.
>>
>> I am going to get a list from AD of users who haven't logged into the
>> network in the past 365 days, export those to a list, and read in
>> from that list in my script to delete the contents of the users
>> folders who appear in the list, but leave the actual user folder
>> itself.
>>
>> "Shay Levi" wrote:
>>
Quote:

>>> If you have more than one value per user you should use csv.
>>>
>>> $DateLimit = (get-date).AddDays(-365)
>>>
>>> #Check last time the folders were wrote to
>>> get-content users.txt | foreach {
>>> $UserDir = "X:\Users\$_"
>>> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
>>> Get-ChildItem $UserDir -rec | remove-item -force
>>> }
>>> }
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>>> I have a script that goes out and deletes the contents of users
>>>> network shares if they have not accessed them in 365 days. It does
>>>> leave the folder itself in case this user deceides to start using
>>>> it again. Instead of going by the last time the folder was wrote
>>>> to, I want to revise the script to read in from an input file with
>>>> a list of users I will query out of Active Directory. This should
>>>> decrease the processing time of tyring to get when the folder was
>>>> last wrote to.
>>>>
>>>> Would it be better to export that list to a text file and use the
>>>> get-content cmdlet?
>>>>
>>>> Or export them to a .CSV file and use the import-csv cmdlet?
>>>>
>>>> Either way I guess would work, but I was trying to figure out how
>>>> to get it to loop so it processes the whole file.
>>>>
>>>> The current script is below:
>>>>
>>>> # Change location to the Users directory
>>>> set-location E:\Users
>>>> #Set the numbers of days back we want to purge
>>>> $DateLimit = (get-date).AddDays(-365)
>>>> #Check last time the folders were wrote to
>>>> foreach ($UserDir in (get-childitem X:\Users | where
>>>> {$_.PSIsContainer})) {
>>>> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
>>>> -gt
>>>> $DateLimit}
>>>> #Remove files and subfolders including read only files
>>>> if (!$newItems) {
>>>> remove-item $UserDir\* -recurse -force
>>>> }
>>>> }

My System SpecsSystem Spec
Old 01-10-2008   #6 (permalink)
JSC
Guest


 

Re: Deleting Users Folder - Script Help

I see now, and using the get-childitem that will leave the users actual main
folder and delete the contents right?

"Shay Levi" wrote:
Quote:

>
> Export users to csv or query them directly from AD and then pipe to foreach.
>
> If you export manually to csv:
>
> import-csv users.csv | foreach {
> # check if the folder exists
> if(test-path "$UserDir\$user" -pathtype container){
> Get-ChildItem "$UserDir\$user" -recurse | remove-item -force
> } else {
> write-warning "$UserDir\$user doesn't exist"
> }
> }
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > I don't have one than one value per user.
> >
> > I just want to read in the list of users who need this procedure done
> > from a file.
> >
> > The the whole section where you check the last time the folder was
> > wrote to can be removed because I can query AD the last time the user
> > logged on to see if they are still active. Guess I didn't explain it
> > very well.
> >
> > I am going to get a list from AD of users who haven't logged into the
> > network in the past 365 days, export those to a list, and read in from
> > that list in my script to delete the contents of the users folders who
> > appear in the list, but leave the actual user folder itself.
> >
> > "Shay Levi" wrote:
> >
Quote:

> >> If you have more than one value per user you should use csv.
> >>
> >> $DateLimit = (get-date).AddDays(-365)
> >>
> >> #Check last time the folders were wrote to
> >> get-content users.txt | foreach {
> >> $UserDir = "X:\Users\$_"
> >> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
> >> Get-ChildItem $UserDir -rec | remove-item -force
> >> }
> >> }
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
> >>> I have a script that goes out and deletes the contents of users
> >>> network shares if they have not accessed them in 365 days. It does
> >>> leave the folder itself in case this user deceides to start using it
> >>> again. Instead of going by the last time the folder was wrote to, I
> >>> want to revise the script to read in from an input file with a list
> >>> of users I will query out of Active Directory. This should decrease
> >>> the processing time of tyring to get when the folder was last wrote
> >>> to.
> >>>
> >>> Would it be better to export that list to a text file and use the
> >>> get-content cmdlet?
> >>>
> >>> Or export them to a .CSV file and use the import-csv cmdlet?
> >>>
> >>> Either way I guess would work, but I was trying to figure out how to
> >>> get it to loop so it processes the whole file.
> >>>
> >>> The current script is below:
> >>>
> >>> # Change location to the Users directory
> >>> set-location E:\Users
> >>> #Set the numbers of days back we want to purge
> >>> $DateLimit = (get-date).AddDays(-365)
> >>> #Check last time the folders were wrote to
> >>> foreach ($UserDir in (get-childitem X:\Users | where
> >>> {$_.PSIsContainer})) {
> >>> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
> >>> -gt
> >>> $DateLimit}
> >>> #Remove files and subfolders including read only files
> >>> if (!$newItems) {
> >>> remove-item $UserDir\* -recurse -force
> >>> }
> >>> }
>
>
>
My System SpecsSystem Spec
Old 01-10-2008   #7 (permalink)
Shay Levi
Guest


 

Re: Deleting Users Folder - Script Help

Correct.

I also had a typo, $user should be $_, here's the modified version:

import-csv users.csv | foreach {
# check if the folder exists
if(test-path "$UserDir\$_" -pathtype container){
Get-ChildItem "$UserDir\$_" -recurse | remove-item -force
} else {
write-warning "$UserDir\$_ doesn't exist"
}
}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> I see now, and using the get-childitem that will leave the users
> actual main folder and delete the contents right?
>
> "Shay Levi" wrote:
>
Quote:

>> Export users to csv or query them directly from AD and then pipe to
>> foreach.
>>
>> If you export manually to csv:
>>
>> import-csv users.csv | foreach {
>> # check if the folder exists
>> if(test-path "$UserDir\$user" -pathtype container){
>> Get-ChildItem "$UserDir\$user" -recurse | remove-item -force
>> } else {
>> write-warning "$UserDir\$user doesn't exist"
>> }
>> }
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>>> I don't have one than one value per user.
>>>
>>> I just want to read in the list of users who need this procedure
>>> done from a file.
>>>
>>> The the whole section where you check the last time the folder was
>>> wrote to can be removed because I can query AD the last time the
>>> user logged on to see if they are still active. Guess I didn't
>>> explain it very well.
>>>
>>> I am going to get a list from AD of users who haven't logged into
>>> the network in the past 365 days, export those to a list, and read
>>> in from that list in my script to delete the contents of the users
>>> folders who appear in the list, but leave the actual user folder
>>> itself.
>>>
>>> "Shay Levi" wrote:
>>>
>>>> If you have more than one value per user you should use csv.
>>>>
>>>> $DateLimit = (get-date).AddDays(-365)
>>>>
>>>> #Check last time the folders were wrote to
>>>> get-content users.txt | foreach {
>>>> $UserDir = "X:\Users\$_"
>>>> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
>>>> Get-ChildItem $UserDir -rec | remove-item -force
>>>> }
>>>> }
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>>>>> I have a script that goes out and deletes the contents of users
>>>>> network shares if they have not accessed them in 365 days. It
>>>>> does leave the folder itself in case this user deceides to start
>>>>> using it again. Instead of going by the last time the folder was
>>>>> wrote to, I want to revise the script to read in from an input
>>>>> file with a list of users I will query out of Active Directory.
>>>>> This should decrease the processing time of tyring to get when the
>>>>> folder was last wrote to.
>>>>>
>>>>> Would it be better to export that list to a text file and use the
>>>>> get-content cmdlet?
>>>>>
>>>>> Or export them to a .CSV file and use the import-csv cmdlet?
>>>>>
>>>>> Either way I guess would work, but I was trying to figure out how
>>>>> to get it to loop so it processes the whole file.
>>>>>
>>>>> The current script is below:
>>>>>
>>>>> # Change location to the Users directory
>>>>> set-location E:\Users
>>>>> #Set the numbers of days back we want to purge
>>>>> $DateLimit = (get-date).AddDays(-365)
>>>>> #Check last time the folders were wrote to
>>>>> foreach ($UserDir in (get-childitem X:\Users | where
>>>>> {$_.PSIsContainer})) {
>>>>> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
>>>>> -gt
>>>>> $DateLimit}
>>>>> #Remove files and subfolders including read only files
>>>>> if (!$newItems) {
>>>>> remove-item $UserDir\* -recurse -force
>>>>> }
>>>>> }

My System SpecsSystem Spec
Old 01-10-2008   #8 (permalink)
JSC
Guest


 

Re: Deleting Users Folder - Script Help

OK, I just checked back because it wasn't working and read that you said you
had a typo. Am I correct on this now?

-------------------------------------
# Change location to the Users directory
set-location x:\Users

import-csv users.csv | foreach {
# check if the folder exists
if(test-path "$UserDir\$_" -pathtype container){
Get-ChildItem "$UserDir\$_" -recurse | remove-item -force -whatif
} else {
write-warning "$UserDir\$_ doesn't exist"
}
}
__________________________

What I was seeing before was it wasn't really taking my csv file into
account and just wanting to delete all the child items of every folder and
the main users folder itself.

"Shay Levi" wrote:
Quote:

> Correct.
>
> I also had a typo, $user should be $_, here's the modified version:
>
> import-csv users.csv | foreach {
> # check if the folder exists
> if(test-path "$UserDir\$_" -pathtype container){
> Get-ChildItem "$UserDir\$_" -recurse | remove-item -force
> } else {
> write-warning "$UserDir\$_ doesn't exist"
> }
> }
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

> > I see now, and using the get-childitem that will leave the users
> > actual main folder and delete the contents right?
> >
> > "Shay Levi" wrote:
> >
Quote:

> >> Export users to csv or query them directly from AD and then pipe to
> >> foreach.
> >>
> >> If you export manually to csv:
> >>
> >> import-csv users.csv | foreach {
> >> # check if the folder exists
> >> if(test-path "$UserDir\$user" -pathtype container){
> >> Get-ChildItem "$UserDir\$user" -recurse | remove-item -force
> >> } else {
> >> write-warning "$UserDir\$user doesn't exist"
> >> }
> >> }
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
> >>> I don't have one than one value per user.
> >>>
> >>> I just want to read in the list of users who need this procedure
> >>> done from a file.
> >>>
> >>> The the whole section where you check the last time the folder was
> >>> wrote to can be removed because I can query AD the last time the
> >>> user logged on to see if they are still active. Guess I didn't
> >>> explain it very well.
> >>>
> >>> I am going to get a list from AD of users who haven't logged into
> >>> the network in the past 365 days, export those to a list, and read
> >>> in from that list in my script to delete the contents of the users
> >>> folders who appear in the list, but leave the actual user folder
> >>> itself.
> >>>
> >>> "Shay Levi" wrote:
> >>>
> >>>> If you have more than one value per user you should use csv.
> >>>>
> >>>> $DateLimit = (get-date).AddDays(-365)
> >>>>
> >>>> #Check last time the folders were wrote to
> >>>> get-content users.txt | foreach {
> >>>> $UserDir = "X:\Users\$_"
> >>>> if ((get-item $UserDir).LastWriteTime -gt $DateLimit) {
> >>>> Get-ChildItem $UserDir -rec | remove-item -force
> >>>> }
> >>>> }
> >>>> -----
> >>>> Shay Levi
> >>>> $cript Fanatic
> >>>> http://scriptolog.blogspot.com
> >>>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
> >>>>> I have a script that goes out and deletes the contents of users
> >>>>> network shares if they have not accessed them in 365 days. It
> >>>>> does leave the folder itself in case this user deceides to start
> >>>>> using it again. Instead of going by the last time the folder was
> >>>>> wrote to, I want to revise the script to read in from an input
> >>>>> file with a list of users I will query out of Active Directory.
> >>>>> This should decrease the processing time of tyring to get when the
> >>>>> folder was last wrote to.
> >>>>>
> >>>>> Would it be better to export that list to a text file and use the
> >>>>> get-content cmdlet?
> >>>>>
> >>>>> Or export them to a .CSV file and use the import-csv cmdlet?
> >>>>>
> >>>>> Either way I guess would work, but I was trying to figure out how
> >>>>> to get it to loop so it processes the whole file.
> >>>>>
> >>>>> The current script is below:
> >>>>>
> >>>>> # Change location to the Users directory
> >>>>> set-location E:\Users
> >>>>> #Set the numbers of days back we want to purge
> >>>>> $DateLimit = (get-date).AddDays(-365)
> >>>>> #Check last time the folders were wrote to
> >>>>> foreach ($UserDir in (get-childitem X:\Users | where
> >>>>> {$_.PSIsContainer})) {
> >>>>> $newItems = Get-ChildItem $UserDir -rec | where {$_.LastWriteTime
> >>>>> -gt
> >>>>> $DateLimit}
> >>>>> #Remove files and subfolders including read only files
> >>>>> if (!$newItems) {
> >>>>> remove-item $UserDir\* -recurse -force
> >>>>> }
> >>>>> }
>
>
>
My System SpecsSystem Spec
Old 01-10-2008   #9 (permalink)
Shay Levi
Guest


 

Re: Deleting Users Folder - Script Help

One thing.. the $UserDir value should be "x:\Users", no need to set location


$UserDir="x:\Users"

import-csv users.csv | foreach {
# check if the folder exists
if(test-path "$UserDir\$_" -pathtype container){
Get-ChildItem "$UserDir\$_" -recurse | remove-item -force -whatif
} else {
write-warning "$UserDir\$_ doesn't exist"
}
}

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: