Schtasks 删除用户配置文件

Schtasks 删除用户配置文件

我有两个脚本,其中一个是批量执行每周任务计划的 task_del_user.cmd

SCHTASKS /Create /SC weekly /D MON,TUE,WED,THU,FRI /TN MyDailyDelete /ST 15:00:00 /TR "Powershell -ExecutionPolicy ByPass -File C:\Users\%username%\Desktop\del_userprofile.ps1" /RU "Admin" /RP "admin1"

第二个是在 powershell 中删除超过 x 天的用户配置文件 del_userprofile.ps1

# Program to delete user profiles through Powershell older than 30 days

# User profiles older than today's date - $ of days will be deleted
$numberOfDays = 30

# Number of digits in local path string to just after C:\users\
$pos = 9

# Get all user profiles where the last log on time is older than the current date - $numberOfDays
$profileStructsToRemove = Get-CimInstance Win32_UserProfile |
    Where-Object {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-$numberOfDays) } |
    Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\ADMINISTRATOR'} |
    Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\PUBLIC'}
    Where-Object {$_.LocalPath.ToUpper() -ne 'C:\USERS\Default'}

foreach ($struct in $profileStructsToRemove)
{
    $userProfileToDelete = $struct.LocalPath.Substring($pos, $struct.LocalPath.Length - $pos)
    Write-Host "Currently deleting profile...$userProfileToDelete..."
    (Get-WmiObject Win32_UserProfile -Filter "localpath='C:\\Users\\$userProfileToDelete'").Delete()
}

我可以在我的帐户上成功完成此操作,但我无法在没有管理权限的会话中应用它,您能帮助我吗?

答案1

您可能需要添加/RL HIGHEST到 schtasks /CREATE 命令。默认值为 LIMITED。

相关内容