# CSV value from an array/collection

**URL:** <https://forums.powershell.org/t/csv-value-from-an-array-collection/20133>\
**Category:** PowerShell Help\
**Created:** [August 11, 2022, 1:36pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133 "2022-08-11T13:36:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![UTIISC1079](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@UTIISC1079](https://forums.powershell.org/u/UTIISC1079)\
**Post date:** [August 11, 2022, 1:36pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/1 "2022-08-11T13:36:39Z")

</div>

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 …

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 11, 2022, 3:10pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/2 "2022-08-11T15:10:20Z")

</div>

Derrick,  
Welcome to the forum. 👋🏽

> [@UTIISC1079](#):
>
> pull a single property value from a collection/array from an object into the .csv file.

> [@UTIISC1079](#):
>
> The properties I need have the “{ }” and only one value in it.

The curly braces indicate that it is in fact a multivalued property. Even if there’s only one value in it. 😉 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. 😉

You may read more about that topic in the

> **[GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best...](https://github.com/PoshCode/PowerShellPracticeAndStyle)**
>
> The Unofficial PowerShell Best Practices and Style Guide - GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best Practices and Style Guide

You may try if this solves your problem:

```auto
$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

```

---

<div class="post-metadata">

**Author:** ![UTIISC1079](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@UTIISC1079](https://forums.powershell.org/u/UTIISC1079)\
**Post date:** [August 11, 2022, 4:48pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/3 "2022-08-11T16:48:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![UTIISC1079](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@UTIISC1079](https://forums.powershell.org/u/UTIISC1079)\
**Post date:** [August 11, 2022, 4:57pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/4 "2022-08-11T16:57:32Z")

</div>

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 😉 )  
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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 11, 2022, 6:52pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/5 "2022-08-11T18:52:09Z")

</div>

> [@UTIISC1079](#):
>
> I apologize for the single line - to me it makes sense.

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

> [@UTIISC1079](#):
>
> 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.

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 …

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

```

My approach …

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

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

```

Both snippets output the same result. 😉

> [@UTIISC1079](#):
>
> 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?

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:

```auto
$AD_FileTime = '126446922000000000'

```

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

```auto
$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:

```auto
$PwShDateTime

```

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

```auto
$PwShDateTime.ToShortDateString()

```

… or you explicitly define the output format by yourself …

```auto
$PwShDateTime.ToString('yyyyMMdd')

```

> [@UTIISC1079](#):
>
> If I just wanted one or certain value(s) out of the collection

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

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

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

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/csv-value-from-an-array-collection/20133/6 "2024-05-16T20:16:46Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


