locked
Script for Profile deletion RRS feed

  • Question

  • Below is my profile deletion powershell script,which should delete profile older then 7 days but on testing I found lots of error.

    $path = "c:\users\"
    $today = get-date
    $folders = Get-ChildItem $path | where { $_.VersionInfo -notlike "File*" }
    foreach ($folder in $folders)
    {
        $start = $folder.LastAccessTime
        $age = (new-TimeSpan -start $start -end $today)
        if (($age.days) -gt "7" )
        {
            $rempath = "$path"+"$folder"
            Write-Host "$rempath to be deleted"
            # Uncomment below to remove items - otherwise list only
            Remove-Item $rempath  -recurse -force -Exclude Public
       
        }
        else
        {
            # Do Nothing
        }
       
    }

    Initial error was cannot delete C:\USers\..\Appdata as access denied. Then when I tried to delete it manually I get an error saying "The source files are larger then is supported by the File System. Try moving to a location which has shorter path name, or try renaming to shorter name(s) before attempting this operation"

    Wondering how can we get this accompolished. Not that good in scripting, but try to get thing from here and there and manage to write some.

    Any help on this will be highly appreciated.

    • Moved by Bill_Stewart Monday, May 18, 2015 7:14 PM This is not "scripts on demand"
    Friday, March 20, 2015 1:22 PM

Answers

All replies

  • If you are not an experienced scripter, don't try to script this.

    Instead, use a tool such as delprof2 which handles all the complexities for you.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Friday, March 20, 2015 2:49 PM
    Friday, March 20, 2015 2:40 PM
  • Agreed, but its not a microsoft tool and asked this question in one of my other forum and they suggested to try scripting...so its round and round :(

    https://social.technet.microsoft.com/Forums/systemcenter/en-US/072ad4d3-c34b-491b-af6c-f0ef23d2d84c/delprof2exe-is-better-then-delprofexe-from-microsoft?forum=winserverGP

    • Edited by CVRajesh Monday, March 23, 2015 5:36 PM
    Monday, March 23, 2015 5:34 PM
  • Sorry, but this is not a script-on-demand forum. You can request a script here, though:

    https://gallery.technet.microsoft.com/scriptcenter/site/requests

    Keep in mind that when you request a script, there's no guarantee that someone has the time to do it for you.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Monday, May 18, 2015 7:14 PM
    Monday, March 23, 2015 6:15 PM
  • Thanks Bill, I understand that. Let me tak it back to microsoft as there policy for profile deletion which doesnt work as required and niether delprof.exe

    Thanks for your input, Bill

    Tuesday, March 24, 2015 10:38 AM
  • In modern Windows profiles are best managed by Group Policy which is why there are no tools to do this anymore. 

    Whenever something seems hard with Windows it is usually because you are taking the wrong approach.  'DelProf' was an old NT4 and earlier tool.  After W2K it was deprecated and GP updated to manage user profiles.

    WMI can delete profiles but is not the preferred method.

    Computer Configuration
    -Policies
    --Administrative Templates
    ---System/User Profiles
    ----"Delete user profiles older than a specified number of days on system restart"


    ¯\_(ツ)_/¯

    Tuesday, March 24, 2015 11:07 AM
  • Yeah, that policy is already in place and not working :)
    Wednesday, March 25, 2015 11:26 AM
  • Yeah, that policy is already in place and not working :)

    Which is why you need to be posting in GP forum to find out how to use GP so that it works.

    ¯\_(ツ)_/¯

    Wednesday, March 25, 2015 12:17 PM
  • It looks like you're running into the path limit of 256 characters (or whatever the limit is).  Instead of doing remove-time, could you try use the DOS command rd /s /q?

    You may have to do something like cmd /c rd /s /q $rempath

    also instead of $rempath = $path + $folder, try $folder.FullName

    Wednesday, March 25, 2015 12:52 PM
  • First, you can't use a "DOS command" because there is no DOS. I think you mean Cmd.exe, not DOS.

    Second, the cmd.exe rd command can delete directories, but not if they are in use by the system.

    Third, even if you can delete the directory, the registry data related to the profile will still be there. The orphaned objects in the file system can cause very strange behavior (Windows expects the directory to be there because of what's in the registry, but you deleted it).

    It is not recommended to simply delete the user profile directory, even if the system allows you to do so.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 25, 2015 2:51 PM
  • I am very well aware of using GP, Sir. Fact is its not working as it wants to be and I have given the reason
    Friday, April 3, 2015 11:29 AM
  • After doing some more research identified more of an problem with the profile itself. Can we asked the script to use ntuser.ini instead of ntuser.dat on profile deletion. I think that will help me solve my problem.
    Friday, April 3, 2015 11:31 AM
  • After doing some more research identified more of an problem with the profile itself. Can we asked the script to use ntuser.ini instead of ntuser.dat on profile deletion. I think that will help me solve my problem.

    There is no way to know what you are trying to ask.  ntuser.ini is not in the profile.  If you cannot delete the profile cleanly then you either have disk corruption or other system problems that need to be resolved.  Fix the system and Group Policy will likely start working.

    You original post indicates that you have an issue with the disk.  The issue with he file being too large to delete is likely an indicator that the disk has a corrupt directory.  You will have to fix that.  If you do not know how to fix these things then you should contact a technician who can help you.

    You should at the very least use CHKDSK on the disk.

    You mention in your other posts that it is the file name that is too long and not the file.  This cannot be solved by scripting.  In you other post DELPROF2 was recommended but you refused to use it. 

    We cannot help you with this.  You will need to learn a bit more about the file system to fix this issue.  It appears you are fighting with one profile with errors.  This is not a scripting issue.

    There third party tools that can help with long paths.  You can also just map  drive halfway down the path and manually delete the files.

    If all of you profiles are like this then I suggest you have malware that needs to be addressed.


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, April 6, 2015 2:23 PM
    Friday, April 3, 2015 11:38 AM
  • This is not a problem of one system..for which I go and run checkdisk..obviously if I am asking for script then it has to  be problem with many system

    Well.. In Short delprof2.exe is the only solution from what I here...Great!!!

    I know I had come across many question. Well thats part of troubleshooting isn't it. Have you not come across this same issue on troubleshooting. For that you dont have to conclude that I dont know this or know that. if you cant answer leave it..other who understand can answer it as required.

    Monday, April 6, 2015 1:01 PM
  • obviously if I am asking for script then it has to  be problem with many system

    Again: The purpose of this forum is to help answer specific scripting questions, not to write scripts on request. The script request page is here:

    http://gallery.technet.microsoft.com/scriptcenter/site/requests


    -- Bill Stewart [Bill_Stewart]

    Monday, April 6, 2015 2:17 PM