-1

I'm very new in powershell. I'm trying to create a script to disable the AD account and do multiple tasks in it.

I'm stuck in the part where i want to assign all the Distribution lists and Security Groups managed by the user to his manager. Below is the script, any help would be appreciated.

$SamAccountName = Read-Host "Enter the username"
$ManagedObjects = Get-ADUser $SamAccountName -Properties managedObjects |select -ExpandProperty managedObjects
$Manager = Get-ADUser $SamAccountName -Properties manager | Select -ExpandProperty Manager
$ManagedObjects |  ForEach-Object {
    Set-ADGroup -Identity $_.Group -ManagedBy ($Manager.DistinguishedName)
}

1 Answers1

0

I have tested in my environment.

You can use the below PowerShell script to change multiple group's managed by to user's manager :

$SamAccountName = Read-Host "Enter the username"
$ManagedObjects = Get-ADUser $SamAccountName -Properties managedobjects | select -ExpandProperty managedObjects
$Manager = Get-ADUser "labadmin" -Properties manager | Select -ExpandProperty Manager
foreach ($group in $ManagedObjects)
{
    Set-ADGroup -Identity $group -ManagedBy $Manager
}
RamaraoAdapa
  • 2,837
  • 2
  • 5
  • 11