我正在尝试创建一个查找所有用户配置文件的脚本。90天或更长时间未访问的配置文件将被删除。我也想从C:\ Users删除他们的文件夹,以释放硬盘空间。
cd C:\Users\
foreach ($dir in $OlderDays) {
Get-CimInstance -ComputerName $ComputerName -Class Win32_UserProfile -ErrorAction Stop | Where-Object {$_.Special -eq $false}
Get-CimInstance -ComputerName $profile.ComputerName -ClassName Win32_UserProfile -ErrorAction Stop | Where-Object {$_.SID -eq $profile.RegKey.SID -and $_.LocalPath -eq $profile.RegKey.LocalPath} | Remove-CimInstance -ErrorAction Stop
Remove-item -force -Path $dir -recurse
}
我收到对试图从C:\ Users删除的每个文件夹的访问被拒绝的信息。
我正在使用管理员帐户。到目前为止,我发现的唯一解决方案是私有化,但是某些工作站需要删除50多个帐户,右键单击文件夹并单击删除需要10秒钟,这不是很节省时间。Takeown每个帐户花费了大约10分钟的时间。
remove-item : Access to the path 'C:\Users\test5456\AppData\Local\Application Data' is denied.
At line:59 char:5
+ remove-item -force -Path C:\Users\$dir -recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Users\test5456:String) [Remove-Item], UnauthorizedAccessExcepti
on
+ FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand
即使您以管理员身份运行它,仍必须拥有该文件夹(及其内容)的所有权,然后才能删除它。
Takeown每个帐户花费了大约10分钟的时间。
众所周知,TakeOwn速度很慢,使用PowerShell的Get-ACL和Set-ACL更快。
要将文件夹及其所有内容的所有者设置为内置的Administrators组,将如下所示(未经测试):
$Group = New-Object System.Security.Principal.NTAccount("Builtin", "Administrators")
$ACL = Get-ACL $dir
$ACL.SetOwner($Group)
Get-ChildItem $dir -Recurse -Force | % {
Set-ACL -AclObject $ACL -Path $_.fullname
}
将其放在Remove-Item命令之前的Foreach循环中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句