CSV value from an array/collection

I am way out of practice here but I am just going to present what I have. I apologize for the misuse of terms. I have been working on this for two weeks but last few days have been all that I did as it is the last item to solve. I am self taught since the classes from college but I do not do this regularly except once a year.
I am trying to pull a single property value from a collection/array from an object into the .csv file. If I am way off, please tell me. I welcome it.

This is what I want in the report (this is the Get-member of the User in Question)

uTAD2000-DS-collegeDepartment Property Microsoft.ActiveDirectory.Management.ADPropertyValueCollection uTAD2000-DS-collegeDepartment {get;set;}

But it outputs

Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Code I have so far is similar to how I fixed the date and time issue
@{N ="uTAD2000-DS-collegeDepartment";E ={[Get-ADResourceProperty -ResultPageSize 1](PSItem."uTAD2000-DS-collegeDepartment")}

The properties I need have the “{ }” and only one value in it. How do I get it to produce?

What I have so far is …

Get-ADGroupMember -id Students.HSC.COM.M1|% {Get-ADUser -id $_ -properties EmployeeID,Organization,departmentNumber,department,cn,GivenName,MiddleName,surName,EmailAddress,Title,uTAD2000-DS-jobBeginDate,uTAD2000-DS-collegeDepartment}| Select-Object -Property EmployeeID,Organization,departmentNumber,department,cn,GivenName,MiddleName,surName,EmailAddress,Title,@{N ="uTAD2000-DS-jobBeginDate";E ={[DateTime]::FromFileTime($PSItem."uTAD2000-DS-jobBeginDate")}},@{N ="uTAD2000-DS-collegeDepartment";E={[Get-ADResourceProperty -ResultPageSize 1](PSItem."uTAD2000-DS-collegeDepartment")}| CovertTo-Csv | Out-file -Path ~\M1.csv -NoTypeInformation

Derrick,
Welcome to the forum. :wave:t4:

The curly braces indicate that it is in fact a multivalued property. Even if there’s only one value in it. :wink: There are 2 common options to deal with it. Either use a loop to iterate through the single values or simply concatenate them to a combined - maybe delimitted - value.

Regardless of that … to put all code in one single line is considered a really bad habbit and makes your code hard to read and maintain. Especially for a beginner I’d recommend to write your code as verbose as possible. Do not use aliasses and always use full parameter names. :wink:

You may read more about that topic in the

You may try if this solves your problem:

$GetAdUserProperties = 
'EmployeeID',
'Organization',
'departmentNumber',
'department',
'cn',
'GivenName',
'MiddleName',
'surName',
'EmailAddress',
'Title',
'uTAD2000-DS-jobBeginDate',
'uTAD2000-DS-collegeDepartment'

$SelectObjectProperties = 
'EmployeeID',
'Organization',
'departmentNumber',
'department',
'cn',
'GivenName',
'MiddleName',
'surName',
'EmailAddress',
'Title',
@{
    Name       = "uTAD2000-DS-jobBeginDate"; 
    Expression = {[DateTime]::FromFileTime($PSItem."uTAD2000-DS-jobBeginDate")}
},
@{
    Name       = "uTAD2000-DS-collegeDepartment"; 
    Expression = {$($PSItem."uTAD2000-DS-collegeDepartment") -join ', '}
}

Get-ADGroupMember -Identity 'Students.HSC.COM.M1' | 
ForEach-Object {
    Get-ADUser -Identity $_ -Properties $GetAdUserProperties
} |
Select-Object -Property $SelectObjectProperties  |
Export-Csv -Path .\M1.csv -NoTypeInformation
2 Likes

Thank you for the clarification. I apologize for the single line - to me it makes sense. I ran what you gave and the jobBegindate is outputing a timestamp but I have no idea from where and majority are at 7:00:00 with a few 7:00:02s sprinkled in.

uTAD2000-DS-jobBeginDate 12/31/1600 7:00:02 PM

Is this due to the rearrange and now I need to be more specific with the datetime format somehow? It was working before. I believe the date is in yyyymmdd
The other question is if I need to modify the datetime to just a date, would I place the line(s) in the calculated expression or do I create another little subsection but before the commands at the bottom?

Using the

-join

I assume is just telling it to take all the values and separate with a comma if needed. (My Occam’s razor is dull sometimes :wink: )
If I just wanted one or certain value(s) out of the collection, could I use like

-index 0,4,6,9

… and pick what I need? And why didn’t/wouldn’t the PageSize work? Wouldn’t this be like using the index and just choosing the first/second/third…etc values in order? I was still inside the Select-Object cmd wasn’t I?

Really? :smirk: What kind of Monitor do you use? The majority of the people I know prefer not to scroll horizontally.

I did not change your code for this calculated property. I just formatted it differently to make it easier to read.
Here you have a nexample:
Your approach …

Get-Service | Select-Object -First 5 -Property Name, Status, @{Name = 'UpperCaseName'; Expression = { $($_.Name).ToUpper() } }

My approach …

$SelectObjectProperty =
'Name',
'Status',
@{
    Name       = 'UpperCaseName'
    Expression = { $($_.Name).ToUpper() }
}

Get-Service | 
    Select-Object -First 5 -Property $SelectObjectProperty

Both snippets output the same result. :wink:

From the AD you get a string representation of an encoded date and time. The idea is to turn it into a proper PowerShell [DateTime] type to be able to format it or calculate with it as needed.

Example: We’ve got something weird looking back from AD:

$AD_FileTime = '126446922000000000'

To make it easier usable we convert it to a proper PwSh [DateTime] type …

$PwShDateTime = [DateTime]::FromFileTime($AD_FileTime)

If you simply output it to the console you will see a date and time in the default format of your system:

$PwShDateTime

Now - since it is a type PwSh knows how to deal with you can use either the integrated default methods to format it …

$PwShDateTime.ToShortDateString()

… or you explicitly define the output format by yourself …

$PwShDateTime.ToString('yyyyMMdd')

The way to access an array element by its index in PwSH is by using square brackets [...].

$Array = 
'John',
'Roger',
'Georg',
'Brian',
'Paul',
'Freddy',
'Ringo',
'John'

$Beatles = $Array[0,2,4,6]
$Queen = $Array[5,3,1,7]
3 Likes