Solved

get-aduser returns Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Posted on 2013-05-28
2
6,984 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 16

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

OfficeMate Freezes on login or does not load after login credentials are input.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
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…

747 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

7 Experts available now in Live!

Get 1:1 Help Now