I am writing a PowerShell script to automate putting users on a leave of absence. As part of this process, we remove the user from their DLs if it is a long absence to stop them from coming back to thousands of emails.

I am planning to saving the list of members to the notes field under the telephone tab, so I can easily restore it manually when they get back (one automation step at a time)

I have no problem pulling a list of of distribution groups with:

 Get-ADPrincipalGroupMembership $user | Where-Object GroupCategory -eq Distribution | select name

The part I run into trouble with is when I try to save it to the notes field with:

Set-ADUser $user -replace @{info=(Get-ADPrincipalGroupMembership $user | Where-Object GroupCategory -eq Distribution |select name)} 

I get the error:

Set-ADUser : Invalid type 'System.Management.Automation.PSObject'.

I have tried massaging the data into shape with -split and -join, but either I'm not doing it right or these are not the right commands. ConvertTo-CSV "works", but adds a lot of extra cruft.

How can I transform this data so that the telephone tab will accept it? My prefered format is something I can use to copy/paste the groups back in afterwords ie. group1;group2;group3 etc. (I will also take comments that the way I am approaching this is all wrong and that method xyz is much better.)


Well, I don't mean to state the obvious, but you are trying to paste in a Powershell object, where the info attribute of an AD user will only accept strings. You need to convert the list of group memberships into a string first, and add the logic yourself for putting your delimiter of choice in between each group name, be it a comma, semicolon or newline character.

Foreach($grp In Get-ADPrincipalGroupMembership $usr | Where-Object GroupCategory -eq Distribution) 
    $GroupString += $grp.Name + ';' 
$GroupString = $GroupString.TrimEnd(';') # Remove the last delimiter off the end
Set-ADUser $usr -Replace @{info=$GroupString}
  • Thanks! I got so fixated on PowerShell having a command for everything that I didn't even think of a loop. Now I know for next time. – David Nov 29 '13 at 21:36

You can also pipe the output to:

Select-Object -ExpandProperty name

This only allows you to select one property, but PowerShell will convert it to a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.