Bulk set ManagedBy; Can't find object in AD

I tried some code and consulted Help for Set-ADGroup but still getting error .

$Groups = Get-Content 'c:\temp\scripts\list.txt' $newMgr = 'CN=ofNewManager'

foreach ($group in $groups){
Set-ADGroup “$($group.ManagedBy)” -ManagedBy $newMgr -WhatIf


Set-ADGroup : Cannot find an object with identity: '' under: 'DC=my,DC=domain'.

$group.ManagedBy is not something you can reference with your code. When using Get-Content alone, you are just reading in strings. They will not have any custom properties. If it is a file with common delimiters between columns or a file with a single column, you can use Import-Csv. By default, Import-Csv expects a header to be present at the top of each column. If it is not present, you can add it with the -Header parameter.

$groups = Import-Csv c:\temp\scripts\list.txt # If you have a ManagedBy header

$groups = Import-Csv c:\temp\scripts\list.txt -Header ManagedBy # If you do not have headers

AdminOfThings45 I created that csv with this format:


Note: all the groups have different names (format of Name); the ManagedBy is the same person in the format of a ‘DN’.

and modified the code as well to this:

$groups = Import-Csv c:\temp\scripts\list.txt
foreach ($group in $groups){ Set-ADGroup $group.group -ManagedBy $group.ManagedBy }

…and it worked, thank you.

If I wanted to first backup the values contained I’m about to replace, I try this code:

$Localpath = 'C:\temp\scripts\groupBackup.csv' $groups = Import-Csv c:\temp\scripts\list.txt
foreach ($group in $groups){ Get-ADGroup $group.group -Properties ManagedBy | Select-Object Name,ManagedBy | Export-Csv -Path $LocalPath -NoTypeInformation }

…but what I end up with is only one group in my export. If I take out the Export-csv, I see displayed in the console all of my groups and ManagedBy’s.

Finally, How did you format your examples? I couldn’t figure out the toolbar for doing it completely, so I used Block quotes.

Hi Jeff,

For your Export-CSV you’ll need to add -Append as at the moment it’s looping through and each loop is overwriting the previous one. If you look at the list of groups in $groups you’re probably ending up with the last group as the final entry in the csv


true enough and thanks.