Solved

get-aduser returns Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Posted on 2013-05-28
2
7,176 Views
Last Modified: 2013-05-29
What is wrong with this script?  It returns "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" for the Dept value.
Import-Module Activedirectory
$users = get-aduser -SearchBase "OU=xxx,DC=us,DC=xxx,DC=com" -Properties userPrincipalName,departmentNumber -filter {userPrincipalName -like "*"}
$csv = foreach($user in $users){

    $grp = get-adprincipalgroupmembership $user
    Foreach($group in $grp){
        New-Object -TypeName PSObject -Property @{
            Dept = $user.departmentNumber
            User = $user.SamAccountName
            Group = $group.Name
            }
        }
}

$csv | Export-csv C:\Users\me\Documents\All_Users.csv
0
Comment
Question by:jasonaluke
2 Comments
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 39203328
Going by the type in your CSV "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" department number would be a multi-string value of some kind, so it can have more than 1 value. If each user will only have 1 value in there just change:
Dept = $user.departmentNumber

Open in new window

To
Dept = $user.departmentNumber[0]

Open in new window

Or you might need to iterate through the department values if there are multiples.
Foreach($group in $grp){ 
        Foreach ($dep in $user.departmentnumber){
            New-Object -TypeName PSObject -Property @{
                Dept = $user.departmentNumber
                User = $user.SamAccountName 
                Group = $group.Name 
            } 
        }
} 

Open in new window

0
 
LVL 2

Author Comment

by:jasonaluke
ID: 39204631
FOLLOW UP:
Simply adding the [0] was the solution.
Thanks
Final script was:
Import-Module Activedirectory
$users = get-aduser -SearchBase "OU=xxx,DC=us,DC=xxx,DC=com" -Properties userPrincipalName,departmentNumber -filter {userPrincipalName -like "*"}
$csv = foreach($user in $users){

    $grp = get-adprincipalgroupmembership $user
    Foreach($group in $grp){
        New-Object -TypeName PSObject -Property @{
            Dept = $user.departmentNumber[0]
            User = $user.SamAccountName
            Group = $group.Name
            }
        }
}

$csv | Export-csv C:\Users\me\Documents\All_Users.csv
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now