我已经创建了我的第一个PowerShell脚本来批量删除用户配置文件。在大多数情况下,它运行良好,但是偶尔会遗留下一些运行Get-CimInstance win32_UserProfile
时在Powershell中找不到的配置文件。
即使PowerShell似乎看不到这些配置文件,但脚本确实会触及它们,因此它们的上次访问日期是当前的,除appdata文件夹以外的所有内容都被删除,并且它们仍位于C:\Users
目录中。
我目前唯一的解决方案是在Users
中手动选择这些配置文件,然后将其删除,但是我试图完全自动化此过程。我对可能导致这种情况的原因不知所措,因此非常感谢任何建议或指导。这是我的代码:
# 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!"
答案 0 :(得分:0)
win10应用程序(MicrosoftOfficeHub)产生了无法正常删除的奇怪链接。当powershell和wmi不起作用时,仍可以使用类似cmd /c rmdir /s /q c:\users\user
的东西。 How to remove user profile completely from the Windows 10 computer?