删除PowerShell中的用户配置文件有时会留下配置文件。

huangapple go评论82阅读模式
英文:

Deleting user profiles in PowerShell occasionally leaves behind profiles

问题

我已创建了我的第一个PowerShell脚本来批量删除用户配置文件,大部分情况下它运行正常,但偶尔会留下一些无法在Powershell中找到的配置文件,当运行Get-CimInstance win32_UserProfile时。

尽管PowerShell似乎看不到这些配置文件,但它们确实被脚本触及,以使它们的最后访问日期是当前的,除了它们的appdata文件夹之外,所有内容都被删除,它们仍然存在于C:\Users目录中。

我目前唯一的解决方法是在Users中手动选择这些配置文件,然后删除它们,但我正在尝试完全自动化这个过程。我对可能引起这个问题感到困惑,因此非常感谢任何建议或指导。以下是我的代码:

# 获取所有用户配置文件并忽略我们的异常情况
$Profiles = Get-CimInstance Win32_UserProfile | where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\admin1") -and ($_.LocalPath -ne "C:\Users\admin2") -and ($_.LocalPath -ne "C:\Users\admin3") -and ($_.LocalPath -ne "C:\Users\admin4"))}

# 获取要在 -PercentComplete 中使用的配置文件数量
$ProfilesCount = $Profiles.Count 

# 此for循环遍历配置文件并删除它们,同时创建我们的进度条
Function ProfilesB-Gone 
{
    for ($i = 1; $i -lt $ProfilesCount; $i++)
    { 
        # 生成并更新我们的进度条 
        Write-Progress -Activity 'Removing Profiles' -Status "Deleted $i out of $ProfilesCount profiles" -PercentComplete (($i/$ProfilesCount) * 100) 
        
        # 在这里,我们抑制错误并继续删除我们的配置文件 
        Remove-CimInstance $Profiles[$i] -EV Err -EA SilentlyContinue 
    }
    # 完成后移除进度条
    Write-Progress -Activity 'Removing Profiles' -Status 'Complete!' -Completed
}

# 调用函数
ProfilesB-Gone;

# 删除由于非关键且不一致的错误而偶尔残留的所有剩余配置文件,并抑制错误。不抑制错误会导致与$ProfilesCount一样多的错误窗口显示。
$Profiles | Remove-CimInstance -EV Err -EA SilentlyContinue 

# 提供成功消息以通知用户脚本已完成
Write-Host "Profiles Deleted!"
英文:

I have created my first PowerShell script to mass delete user profiles, For the most part it works fine, however, it does occasionally leave behind some profiles that cannot be found in Powershell when Get-CimInstance win32_UserProfile is run.

Even though PowerShell seemingly does not see these profiles they do get touched by the script so that their last accessed date is current, everything aside from their appdata folders are deleted, and they are still in the C:\Users directory.

The only solution I have currently is to manually select these profiles within Users and then delete them but I'm trying to completely automate this process. I am at a loss as to what could cause this, so any advice or guidance is greatly appreciated. Here is my code:

# Grab all the user profiles and ignore anything in our exceptions
$Profiles = Get-CimInstance Win32_UserProfile | where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\admin1") -and ($_.LocalPath -ne "C:\Users\admin2") -and ($_.LocalPath -ne "C:\Users\admin3") -and ($_.LocalPath -ne "C:\Users\admin4"))}

# Get the number of profiles to use in -PercentComplete
$ProfilesCount = $Profiles.Count 

# This for loop iterates through profiles and deletes them as well as creates our progress bar
Function ProfilesB-Gone 
{
    for ($i = 1; $i -lt $ProfilesCount; $i++)
    { 
        # Our progress bar is generated and updated 
        Write-Progress -Activity 'Removing Profiles' -Status "Deleted $i out of $ProfilesCount profiles" -PercentComplete (($i/$ProfilesCount) * 100) 
        
        # Here we're suppressing errors and continuing while deleting our profiles 
        Remove-CimInstance $Profiles[$i] -EV Err -EA SilentlyContinue 
    }
    # Remove progress bar once complete
    Write-Progress -Activity 'Removing Profiles' -Status 'Complete!' -Completed
}

# Call function
ProfilesB-Gone;

# Remove all leftover profiles that remain ocasionally due to noncritical and inconsistent bug and suppress errors. Not suppressing errors causes as many error windows to show as there were $ProfilesCount.
$Profiles | Remove-CimInstance -EV Err -EA SilentlyContinue 

# Give success message to inform user of script completion
Write-Host "Profiles Deleted!"

答案1

得分: 0

一款Win10应用程序(MicrosoftOfficeHub)创建了一些无法通过常规方式删除的奇怪链接。使用类似cmd /c rmdir /s /q c:\users\user这样的命令仍然有效,而PowerShell和WMI则不行。https://stackoverflow.com/questions/57198252/how-to-remove-user-profile-completely-from-the-windows-10-computer/57201724#57201724

英文:

A win10 app (MicrosoftOfficeHub) makes strange links that can't be deleted in the normal way. Using something like cmd /c rmdir /s /q c:\users\user still works, when powershell and wmi don't. https://stackoverflow.com/questions/57198252/how-to-remove-user-profile-completely-from-the-windows-10-computer/57201724#57201724

huangapple
  • 本文由 发表于 2020年1月3日 23:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定