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

时间:2019-07-25 09:09:11

标签: powershell powershell-3.0

我想通过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

是否有任何解决方法/黑客删除用户/ AppData文件夹。

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

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

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

我要删除的命令如下

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

我以管理员身份运行PowerShell。

谢谢。

2 个答案:

答案 0 :(得分:0)

相关的错误报告:https://github.com/powershell/powershell/issues/621我自己已经看到了。稍后再详细研究。要完全删除配置文件,需要在文件资源管理器中进行多次尝试。

编辑:

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

说我以这种方式删除个人资料:

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

剩下几个文件和文件夹。他们来了。我认为有些链接?为了简洁起见,我将列出这些文件。

PS C:\Users> ls -r -force user -file


    Directory: C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\Local\Microsoft\CLR_v4.0\UsageLogs


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/27/2019  12:20 PM           1253 LocalBridge.exe.log


    Directory: C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft\CryptnetUrlCache\Content


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a--sl        7/27/2019  12:16 PM         (1507) 6BADA8974A10C4BD62CC921D13E43B18_D9817BD5013875AD517DA73475345203


    Directory: C:\Users\user\AppData\Local\Packages\Microsoft.MicrosoftOfficeHub_8wekyb3d8bbwe\LocalCache\LocalLow\Microsoft\CryptnetUrlCache\MetaData


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a--sl        7/27/2019  12:17 PM          (438) 6BADA8974A10C4BD62CC921D13E43B18_D9817BD5013875AD517DA73475345203

答案 1 :(得分:0)

@( 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类的文档

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

更好的方法是比较SID:

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