How to remove user profile completely from the Windows 10 computer?

前端 未结 2 1850
长发绾君心
长发绾君心 2021-01-07 09:40

I want to remove local user in windows 10 computer through powershell.

I have tried command

Get-WMIObject -class Win32_UserProfile | Where {((!$_.Sp         


        
相关标签:
2条回答
  • 2021-01-07 09:48
    @( Get-WMIObject -class 'Win32_UserProfile' -ComputerName 'Terminal001.domain.local' )| 
        Where-Object { <**some filter conditions**> } | 
        Where-Object { $_.Special -eq $false } | 
        Where-Object { $_.Loaded  -eq $false } | 
        Where-Object { $_.Status  -eq 2 } | # We remove only local copies of Roaming profiles. We assume there is a copy of Roaming profile on a server.  
        Foreach-Object {
            $local:profile = $_;
            Write-Host -f "Gray" "Deleting $($_.LocalPath)`t" -noNewLine
            try {
                $local:profile.Delete()
                Write-Host "OK!" -f Green
            } catch {
                Write-Host -f "Yellow" "Unable to delete $($local:profile.LoalPath) : $($_.Exception.Message)"
            }
        }
    

    For Status see docs for Win32_UserProfile class

    Please take a note, that LocalPath is not always equal username. For example, if username changed while profile is created already, LocalPath does not change. Or, else, profile folder can be named not Username, but Username.Userdomain, or UserName.UserDomain.nnn (nnn - number).

    Better way is to compare SIDs:

    $userSID = (Get-ADUser 'username').SID
    Get-WmiObject -Class 'Win32_UserProfile' .... |
    Where-Object {$_.SID -eq $userSID} |
    ...
    
    0 讨论(0)
  • 2021-01-07 10:10

    EDIT: Use cmd.

    cmd /c rmdir /s /q c:\users\user
    

    Powershell can't handle bad links in directories. Uninstall that terrible "Office" app. The problem is under AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache. There's directory junctions (?) with null linktype and target properties.

    Windows 10 has a unique problem in version 1809 with the "Office" app creating strangely linked files and directories under the user profile. Even WMI can't delete the profile, nor powershell.

    Wow, what kind of directory junction has no target or linktype?? But it has a ReparsePoint attribute and a mode with an "l" on the end for link.

    get-item c:\users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\localcache | fl *
    
    PSPath            : Microsoft.PowerShell.Core\FileSystem::C:\users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\localcache
    PSParentPath      : Microsoft.PowerShell.Core\FileSystem::C:\users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe
    PSChildName       : localcache
    PSDrive           : C
    PSProvider        : Microsoft.PowerShell.Core\FileSystem
    PSIsContainer     : True
    Mode              : d----l
    BaseName          : localcache
    Target            : {}
    LinkType          :
    Name              : localcache
    FullName          : C:\users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\localcache
    Parent            : Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe
    Exists            : True
    Root              : C:\
    Extension         :
    CreationTime      : 8/1/2019 3:29:02 PM
    CreationTimeUtc   : 8/1/2019 7:29:02 PM
    LastAccessTime    : 8/1/2019 3:29:02 PM
    LastAccessTimeUtc : 8/1/2019 7:29:02 PM
    LastWriteTime     : 8/1/2019 3:29:02 PM
    LastWriteTimeUtc  : 8/1/2019 7:29:02 PM
    Attributes        : Directory, ReparsePoint
    

    Related bug report: https://github.com/powershell/powershell/issues/621 I've seen this myself. It takes multiple attempts in file explorer to completely delete the profile.

    EDIT:

    Win 10 1809: WMI Can't Fully Delete Profiles (because of Microsoft.MicrosoftOfficeHub)

    Say I delete a profile this way:

    Get-CimInstance win32_userprofile | where localpath -match user$ | Remove-CimInstance
    

    Several folders get left over. Here they are. Remove-item says "is an NTFS junction point. Use the Force parameter to delete or modify this object." Remove-item -force says "There is a mismatch between the tag specified in the request and the tag present in the reparse point"

    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\Local
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\Local\Microsoft
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\Local\Microsoft\CLR_v4.0
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\Local\Microsoft\CLR_v4.0\UsageLogs
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft\CryptnetUrlCache
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft\CryptnetUrlCache\Content
    C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft\CryptnetUrlCache\MetaData
    
    0 讨论(0)
提交回复
热议问题