![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #8 (permalink) | ||||||||||||||||||||||||||||||||||||
| 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:
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #9 (permalink) |
| 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: |