Windows用户删除脚本

发布于 2025-02-13 01:16:55 字数 679 浏览 0 评论 0原文

我需要有关此代码的一些帮助。从理论上讲,该代码可行,但是当代码运行所有用户配置文件以外的所有用户配置文件时,我的排除量将被删除。我希望最后一次登录配置文件保留在30天内,并在30天内删除用户。我尝试了不同的addday( - ),但是问题仍然存在。

谁能帮忙?

$ExcludedUsers = "ExcludedFolder"
$LocalProfiles = Get-WMIObject -class Win32_UserProfile | Where-Object { (!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-30)) }
foreach ($LocalProfile in $LocalProfiles) {
    if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace("C:\Users\", ""))) {
        $LocalProfile | Remove-WmiObject
        Write-host $LocalProfile.LocalPath, "Profile Has Been Deleted” -ForegroundColor Red
    }
}

感谢您的帮助

I need some help with this code. The code, in theory, works however, when the code runs all user profiles apart from the ones in my excluded are deleted. I want the last login profiles to remain within 30 days and users over 30 days to be deleted. I have tried different AddDays(-) however the problem remains.

Could anyone help?

$ExcludedUsers = "ExcludedFolder"
$LocalProfiles = Get-WMIObject -class Win32_UserProfile | Where-Object { (!$_.Special) -and (!$_.Loaded) -and ($_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-30)) }
foreach ($LocalProfile in $LocalProfiles) {
    if (!($ExcludedUsers -like $LocalProfile.LocalPath.Replace("C:\Users\", ""))) {
        $LocalProfile | Remove-WmiObject
        Write-host $LocalProfile.LocalPath, "Profile Has Been Deleted” -ForegroundColor Red
    }
}

I appreciate your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文