AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1438183
Accepted
fstab2323234
fstab2323234
Asked: 2019-05-18 10:44:47 +0800 CST2019-05-18 10:44:47 +0800 CST 2019-05-18 10:44:47 +0800 CST

powershell 删除文件夹而不取得所有权?

  • 772

我正在尝试创建一个脚本来查找所有用户配置文件。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

以管理员身份运行

powershell windows-8
  • 4 4 个回答
  • 4889 Views

4 个回答

  • Voted
  1. Best Answer
    Ƭᴇcʜιᴇ007
    2019-05-18T11:08:41+08:002019-05-18T11:08:41+08:00

    即使您以管理员身份运行它,您仍然必须取得该文件夹(及其内容)的所有权才能删除它。

    Takeown 每个帐户大约需要 10 分钟。

    TakeOwn 是出了名的慢,使用 PowerShell 的 Get-ACL 和 Set-ACL 更快。

    要将文件夹及其所有内容的所有者设置为内置的管理员组,应该是这样的(未测试):

    $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
    }
    

    将它放在 Foreach 循环中,在 Remove-Item 命令之前。

    • 1
  2. Appleoddity
    2019-05-18T12:01:35+08:002019-05-18T12:01:35+08:00

    你不需要这样做。Windows 将使用名为的组策略为您完成此操作:

    在指定的天数后删除用户配置文件。

    此外,已经提供了一个工具来执行此操作delprof,delprof2如果您想手动执行此操作。它也可以在远程计算机上执行。

    • 1
  3. Daniel Cogny
    2020-12-22T06:40:29+08:002020-12-22T06:40:29+08:00

    推荐的解决方案主要对我有用,但是:

    • 大多数情况下,这意味着将近 50% 的个人资料仍然无法获得权限,因此不会被删除。
    • 与以下解决方案相比,每个项目花费的时间相当长:

    对我有用的是cmd /c "rmdir $dir /s /q"用作删除命令,因此在您的情况下它将是:

    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 
    
        cmd /c "rmdir $dir /s /q"
    }
    
    

    这个解决方案要快得多,到目前为止,它已经在一台计算机上删除了一百多个配置文件而没有出现任何故障。凭借 PowerShell 的强大功能,我们不得不求助于调用 cmd 来完成工作,这是一种耻辱,但是......

    解决方案在这里找到:https ://forums.ivanti.com/s/article/Folder-delete-actions-fail-when-the-folder-contains-a-Junction-soft-link?language=en_US

    • 1
  4. Christopher Hostage
    2019-05-18T11:32:01+08:002019-05-18T11:32:01+08:00

    正如@Ramhound 评论的那样,如果您要删除旧的用户配置文件,请不要通过删除 C:\Users\username 来完成。首先,从 PC 中删除配置文件并等待几分钟。如果它工作正常,删除配置文件将自动删除文件夹结构。如果配置文件仍然存在,那么尝试像您尝试的那样删除文件夹会引发各种权限错误。

    相关内容请见

    https://www.business.com/articles/powershell-manage-user-profiles/

    https://community.spiceworks.com/how_to/124316-delete-user-profiles-with-powershell

    Power Shell 脚本将用户配置文件从 Windows Server 2008 r2 删除到本地计算机(Windows 7)

    • 0

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve