Solved

get-aduser returns Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Posted on 2013-05-28
2
7,779 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
This article explains how to install and use the NTBackup utility that comes with Windows Server.
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 walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

679 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