用于删除AD计算机上用户的文件夹的PowerShell脚本

时间:2017-07-11 17:46:01

标签: powershell active-directory

我一直在尝试创建一个脚本,该脚本从最初从PDQ部署创建的所有AD计算机中以管理员名称删除C:\ Users中的文件夹。当我尝试运行它时,我收到有关丢失文件路径的错误。搜索结果产生了一个很长的脚本,虽然我当前的脚本只有几行。我希望它尽可能轻量级,因此性能不会严重降低。我错过了什么吗?代码如下。

$ Destination ='C:\ Users \ foldername'

Get-ChildItem $ Destination | ForEach-Object {remove-Item -Force -recurse}

1 个答案:

答案 0 :(得分:0)

假设您拥有权限,可能会有所作为,但使用通配符过滤器运行Get-ADComputer可能需要很长时间,具体取决于有多少

$allComputers = Get-ADComputer -filter "Name -like `"*`"" | Select Name
$Destination = "C:\Users\folder"
Foreach($computer in $allComputers.Name)
{
    Invoke-Command -computername $computer -ScriptBlock {
        param($Destination)
        if(Test-Path ($Destination)){
            Write-Host "Removing $Destination from $computer"
            Remove-Item $Destination -Force -Recurse
        }
    } -ArgumentList $Destination
}
相关问题