Tuesday, 20 June 2023

PowerShell - How to remove multiple computer objects from multiple groups in Active Directory using a CSV.

 PowerShell - How to remove multiple computer objects from multiple groups in Active Directory using a CSV.



File Format :- CSV file




# Specify the path to the CSV file

$csvFilePath = "C:\path\to\ComputerAndGroup.csv"

 

# Read the CSV file

$csvData = Import-Csv -Path $csvFilePath

 

# Iterate through each row in the CSV

foreach ($row in $csvData) {

    # Get the group name and computer name from the CSV

    $groupName = $row.GroupName

    $computerName = $row.ComputerName

 

    # Get the group object

    $group = Get-ADGroup -Identity $groupName

 

    # Get the computer object

    $computer = Get-ADComputer -Identity $computerName

 

    # Remove the computer from the group

    Remove-ADGroupMember -Identity $group -Members $computer -Confirm:$false

 

    # Output the result

    Write-Host "Removed computer $computerName from group $groupName"

}

No comments:

Post a Comment