Jump to content

Recommended Posts

Posted (edited)

Hi I have the following script for cleaning profiles but I can't make it work with Windows 7. I think Windows 7 handles profiles differently by creating registry entries... Does anybody have any links to a windows 7 profile cleaning script?


ON ERROR RESUME NEXT
' Clears user profiles on computer startup
' leaves the following folders
' Administrator
' RM Default User
' Default User
' All Users
' Reference : http://msdn2.microsoft.com/en-us/library/9kcx47hd.aspx
dim oktodelete, fso, f, foldercollection
set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.getfolder("C:\Users")
set foldercollection = f.subfolders
for each folder in foldercollection
oktodelete = true
if instr(1, folder.path, "Administrator") then
oktodelete = false
end if
if instr(1, folder.path, "Default") then
oktodelete = false
end if
if instr(1, folder.path, "Public") then
oktodelete = false
end if
if instr(1, folder.path, "Administrator.MYDOMAIN") then
oktodelete = false
end if
if instr(1, folder.path, "LocalService") then
oktodelete = false
end if
if instr(1, folder.path, "NetworkService") then
oktodelete = false
end if
if oktodelete then
'wscript.echo "Deleting Folder : " & folder.path
fso.deletefolder folder.path, true
end if
next

Edited by Yzöwl
code tags added for readability

Posted

Try this script, it will only list the folders in C:\Users and produce a text file with the results.

Save As ListUserFolders.vbs


Option Explicit
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Dic :Set Dic = CreateObject("Scripting.Dictionary")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Wmi :Set Wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim Loc :Loc = "C:\Users"
Dim Dir :Dir = Array(Loc & "\All Users",Loc & "\Default",Loc & "\Default User",Loc & "\Public")
Dim C1, I, K, Obj, Ts, Tx
'-> Text File For Listing Results
Tx = Act.SpecialFolders("Desktop") & "\" & _
Act.ExpandEnvironmentStrings("%ComputerName%") & "_UserFolders.txt"
Set Ts = Fso.CreateTextFile(Tx)
Ts.WriteLine Now & vbCrLf
C1 = 0
'-> Add The Path That Should Not Be Deleted
For Each Obj In Dir
If Fso.FolderExists(Obj) Then Dic.Add Obj,Obj
Next
'-> Gather Local Account, Build A Path To Add To The Dictionary
For Each Obj In Wmi.InstancesOf("Win32_UserAccount")
If Not Obj.Disabled Then
If Fso.FolderExists(Loc & "\" & Obj.Name) Then
Dic.Add Loc & "\" & Obj.Name,Loc & "\" & Obj.Name
End If
Else
If Fso.FolderExists(Loc & "\" & Obj.Name) Then
'-> Account Is Disable And The Folder Still Exists
Dic.Add Loc & "\" & Obj.Name,Loc & "\" & Obj.Name
End If
End If
Next
'-> Loop Threw Users Folder If The Folder Not In The Dictionary Delete
For Each Obj In Fso.GetFolder(Loc).SubFolders
If Not Dic.Exists(Obj.Path) Then
C1 = 1
'-> Delete Code Here
Ts.WriteLine "Deleted This Folder." & vbCrLf & Obj.Path
End If
Next
'-> Check To See If Any Folders Where Deleted
If C1 = 0 Then
Ts.WriteLine "There Where No Folders That Where Deleted"
Ts.Close
Else
Ts.Close
End If
'-> Read The Results
Act.Run(Chr(34) & Tx & Chr(34)),1,True
'_> Delete The Text File
Fso.DeleteFile(Tx),True

Posted

You won't be able to simply delete any User folders on Windows 7. You will need to change your script to add a sub that will first take ownership + grant privileges, before you can delete those unneeded user folders.

Posted

You won't be able to simply delete any User folders on Windows 7. You will need to change your script to add a sub that will first take ownership + grant privileges, before you can delete those unneeded user folders.

Yeah I thought it might be down to permissions... Do you know of any examples to get me started?

Posted

You will have to convert this to VBS, but here are the commands

takeown /F "c:\Users\Username"

icacls c:\Users\Username /grant administrators:F

Posted

You could add the extra code here

'-> Delete Code Here       Ts.WriteLine "Deleted This Folder." & vbCrLf & Obj.Path

Change to this

'-> Delete Code Here     Act.Run("takeown /A /f "  & Chr(34) & Obj.Path & chr(34)),1,True   Act.Run("icacls " & Chr(34) & Obj.Path & chr(34) & " /grant administrators:F"),1,True   Ts.WriteLine "Deleted This Folder." & vbCrLf & Obj.Path
Posted

You could add the extra code here

'-> Delete Code Here

Ts.WriteLine "Deleted This Folder." & vbCrLf & Obj.Path

Change to this

'-> Delete Code Here

Act.Run("takeown /A /f " & Chr(34) & Obj.Path & chr(34)),1,True

Act.Run("icacls " & Chr(34) & Obj.Path & chr(34) & " /grant administrators:F"),1,True

Ts.WriteLine "Deleted This Folder." & vbCrLf & Obj.Path

Thanks I will give that a try!

Posted (edited)

Ok so I have done this:

But it don't seem to do anything!



ON ERROR RESUME NEXT

dim oktodelete, fso, f, foldercollection
set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.getfolder("C:\Users")
set foldercollection = f.subfolders
for each folder in foldercollection

if instr(1, folder.path, "user1") then
oktodelete = true

else
oktodelete = false


end if

if instr(1, folder.path, "User2") then
oktodelete = true

else
oktodelete = false


end if



if oktodelete then
'wscript.echo "Deleting Folder : " & folder.path
Act.Run("takeown /A /f " & Chr(34) & folder.Path & chr(34)),1,True
Act.Run("icacls " & Chr(34) & folder.Path & chr(34) & " /grant administrators:F"),1,True
fso.deletefolder folder.path, true
end if
next

Edited by gunsmokingman
Added Code Tags For Format
Posted

Try this script see if it works

 Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Dim Obj'-> Loop Threw The User Folders   For Each Obj In Fso.GetFolder("C:\Users").SubFolders    If InStr(1,Obj.Path, "user1",1) Then      DelTheFolder(Obj.Path)    End If    If InStr(1,Obj.Path, "User2",1) Then      DelTheFolder(Obj.Path)    End If   Next'-> Delete The Folder   Function DelTheFolder(Del)'-> Message Box That Will Close In 3 Seconds And Continue With The Functions    Act.Popup "Preparing To Delete This Folder" & vbCrLf & Del, 3, "Del Folder",4128     Act.Run("takeown /A /f "  & Chr(34) & Del & chr(34)),1,True              Act.Run("icacls " & Chr(34) & Del & chr(34) & " /grant administrators:F"),1,True      Fso.DeleteFolder(Del),True   End Function
Posted

Try this script see if it works


Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Obj
'-> Loop Threw The User Folders
For Each Obj In Fso.GetFolder("C:\Users").SubFolders
If InStr(1,Obj.Path, "user1",1) Then
DelTheFolder(Obj.Path)
End If
If InStr(1,Obj.Path, "User2",1) Then
DelTheFolder(Obj.Path)
End If
Next
'-> Delete The Folder
Function DelTheFolder(Del)
'-> Message Box That Will Close In 3 Seconds And Continue With The Functions
Act.Popup "Preparing To Delete This Folder" & vbCrLf & Del, 3, "Del Folder",4128
Act.Run("takeown /A /f " & Chr(34) & Del & chr(34)),1,True
Act.Run("icacls " & Chr(34) & Del & chr(34) & " /grant administrators:F"),1,True
Fso.DeleteFolder(Del),True
End Function

Same problem occurs "Permission Denied Error" :wacko:

Posted

Is this being run under the local admin account, or an account that is not the local admin but has admin privileges?

Yes I am logged in as a Domain Adminstrator when trying to run this.. Still get permission denied error. The profile is not in use so I can't see why it doesn't allow me to delete it. If I right click on the profile folder and select delete it deletes without a problem.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...