Solved

get-aduser returns Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange password change on mobile 2 92
exchange, windows server 2008 4 53
check AD account creation and disabled 8 34
IF statement on a PowerShell Script 2 25
A procedure for exporting installed hotfix details of remote computers using powershell
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
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…

738 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