Solved

get-aduser returns Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Posted on 2013-05-28
2
8,286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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…
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

627 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