如何从 Windows 10 计算机中完全删除用户配置文件?

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

我想通过 powershell 删除 windows 10 台计算机中的本地用户。

我试过命令

Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -eq "C:\Users\$user")  -and ($_.LocalPath -ne "C:\Users\UpdatusUser"))} | Remove-WmiObject

以上命令成功删除本地用户。但是不能删除C:\Users$user\AppData

有没有workaround/hack删除user/AppData文件夹。

我也尝试过在成功删除用户后删除文件夹,但出现错误。

错误:请求中指定的标签与重解析点中存在的标签不匹配

对于错误参考,我尝试了一些针对该错误消息的解决方法 none 有效。但是,如果我执行 shift+delete(手动),它将成功删除。

我用来删除的命令如下

Remove-Item C:\User$user -Force -Recurse

我是 运行 PowerShell 管理员。

提前致谢。

编辑:使用 cmd。

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

Powershell 无法处理目录中的错误 link。卸载那个可怕的 "Office" 应用程序。问题在 AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache 下。存在具有 null linktype 和 target 属性的目录连接 (?)。

Windows 10 在版本 1809 中有一个独特的问题,"Office" 应用程序在用户配置文件下创建了奇怪的 linked 文件和目录。即使是 WMI 也不能删除配置文件,powershell 也不能。

哇,什么样的目录连接没有目标或 link类型??但是它有一个 ReparsePoint 属性和一个在 link.

末尾带有 "l" 的模式
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

相关错误报告:https://github.com/powershell/powershell/issues/621我亲眼所见。在文件资源管理器中需要多次尝试才能完全删除配置文件。

编辑:

Win 10 1809:WMI 无法完全删除配置文件(因为 Microsoft.MicrosoftOfficeHub)

假设我这样删除个人资料:

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

遗留了几个文件夹。他们来了。 Remove-item 说 "is an NTFS junction point. Use the Force parameter to delete or modify this object." Remove-item -force 说 "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
@( 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)"
        }
    }

对于 Status 请参阅 Win32_UserProfile class

的文档

请注意,LocalPath 并不总是等于用户名。例如,如果在创建配置文件时更改了用户名,则 LocalPath 不会更改。或者,配置文件文件夹可以不命名为 Username,而是命名为 Username.Userdomain,或 UserName.UserDomain.nnn(nnn - 数字)。

更好的方法是比较 SID:

$userSID = (Get-ADUser 'username').SID
Get-WmiObject -Class 'Win32_UserProfile' .... |
Where-Object {$_.SID -eq $userSID} |
...