Author Posts

January 18, 2017 at 10:54 pm

Hi,

I needed a power shell script that would pull all the members out of a group and pipe them to a CSV. I could get it to work if I used FT (format command to the screen but I remember we cannot pipe formatted stuff to csv)

I figured out how to do this but it was kind of corny, and I don't think it was done properly. I don't really study powershell anymore as the last year or so has been spent getting my CCNA and now working on my CCNP. I love Power Shell and use to study it but I never got to use much of it at work which is why I pushed hard in the network direction.

Anyway my code below worked but but put a bunch of crud into the csv and I cheated by then saving it as a Excel workbook and just deleting out the columns I did not want.

Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv

What would be a much cleaner and better way to do this where my outputted information was only the properties that I wished instead of all the generic properties?

January 19, 2017 at 12:52 am

If I understand correctly, you just want the Name,Email in the CSV?
You can pipe to select.

Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | Select name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv

I personally don't like piping everything in a script. I would write it like this, but i'm sure someone else could probably write it cleaner.

$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}

January 19, 2017 at 2:29 pm

I got the first one you gave me to work perfectly the second one I was looking at but it gave me an error:

$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}

Get-ADUser : Invalid type 'Microsoft.ActiveDirectory.Management.ADPrincipal'.
Parameter name: distinguishedName
At line:5 char:1
+ Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Sel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 

I was thinking maybe the word DistinguishedName was spelled incorrectly but I looked a user up under Attribute Editor in the Active directory and that looks to be the correct Attribute name and spelling.

I found this post that said something about a bug its very close to the same error but not sure if that is related or not but figured I would share the website.

https://blogs.technet.microsoft.com/pstips/2014/12/02/get-aduser-one-or-more-properties-are-invalid-parameter-name-msds-assignedauthnpolicy/

January 27, 2017 at 7:11 pm

un-tested as I don't have domain to play with, but this little tweak should do the trick. I'd say that using
$member.DistinguishedName would not work inside the filter as ad module is a bummer.

$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
$dn = $member.DistinguishedName
Get-ADUser -Filter {DistinguishedName -like $dn} -Properties name,mail | Select name,mail
}

It could also work like this:

$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members.DistinguishedName){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}