gpt4 book ai didi

powershell - 简单的脚本删除旧的用户配置文件

转载 作者:行者123 更新时间:2023-12-03 00:13:19 25 4
gpt4 key购买 nike

我正在编写一个简单的脚本,该脚本将用于删除90天以上的用户个人资料。我可以捕获所需的配置文件,但是当涉及“面包和黄油”时,我感到很困惑。

我的代码:

$localuserprofiles = Get-WmiObject -Class Win32_UserProfile | Select-Object localPath,@{Expression={$_.ConvertToDateTime($_.LastUseTime)};Label="LastUseTime"}| Where{$_.LocalPath -notlike "*$env:SystemRoot*"} #Captures local user profiles and their last used date
$unusedday = 90 # Sets the unused prifile time threshold
$excludeduserpath = $excludeduser.LocalPath # Excludes the DeltaPC user account
$profilestodelete = $LocalUserProfiles | where-object{$_.lastusetime -le (Get-Date).AddDays(-$unusedday) -and $_.Localpath -notlike "*$excludeduserpath*"} #Captures list of user accounts to be deleted

#Deletes unused Profiles

Foreach($deletedprofile in $profilestodelete)
{
$deletedprofile.Delete()
}

代码返回此错误:
Method invocation failed because [Selected.System.Management.ManagementObject] does not contain a method named 'Delete'. 
At line:3 char:13
+ $deletedprofile.Delete()}
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

最佳答案

对于其他答案,WMI没有为用户配置文件提供Delete()方法。尽管您可以删除配置文件目录,但是通常不建议这样做,因为这样会留下其他各种数据(例如注册表项),并且如果这些用户随后重新登录到计算机,则可能导致登录问题。

有一个名为delprof2.exe的免费第三方工具:https://www.sepago.com/blog/2011/05/01/new-free-delprof2-user-profile-deletion-tool

我没有亲自使用过,因此请谨慎使用,但似乎已经可以选择删除x天不 Activity 的个人资料,例如:

Delprof2 /d:90

Now, what happens if you simply delete the user profile directory below C:\Users without modifying the registry? The next time the user logs on Windows displays a balloon tip whining that Windows could not load the user profile and that the user was logged on with a temporary profile. Is that bad? Yes! Temporary profiles are a last resort if Windows cannot load the user profile. Upon logoff they are deleted and all data is lost. That certainly is a reason to avoid them.

关于powershell - 简单的脚本删除旧的用户配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com