# Custom object array copied from the book doesn't work

**URL:** <https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560>\
**Category:** Uncategorized\
**Created:** [September 19, 2026, 6:15am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560 "2026-09-19T06:15:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alan.revera](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alan.revera](https://forums.powershell.org/u/alan.revera)\
**Post date:** [September 19, 2026, 6:15am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/1 "2026-09-19T06:15:48Z")

</div>

I copied this from the book and try to input into PS, it gives me error. Here is the script

$people = @(

[PSCustomObject] @(Name= ‘John’; age = 30; occupation = ‘engineer’)

[PSCustomObject] @(Name =’Alice’; age =25; occupation = ‘designer’)

[PSCustomObject] @(Name =’Bob’; age = 40; occupation = ‘Manager’)

)

It doesn’t recognize Name age occupation. I tried to put in $Name=‘Alan’, $age=15, occupation=‘none’ in front of this to define them as variables, still won’t work.

Please help

Thanks

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 19, 2026, 8:16am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/2 "2026-09-19T08:16:53Z")

</div>

Firstly, when posting code, data, or output in the forum, please can you use the pre-formatted text button. It looks like \</\> in the toolbar. It really helps us with readability, and copying and pasting your code (we don’t have to faff about replacing curly quote marks to get things working).

Either the book is wrong, or you have copied the code incorrectly. You need to use curly brackets (braces - `{}` ), rather than round brackets (parentheses - `()` ) when using the `[PSCustomObject]` type accelerator.

```PowerShell
$people = @(
    [PSCustomObject] @{Name= 'John'; age = 30; occupation = 'engineer'}
    [PSCustomObject] @{Name ='Alice'; age =25; occupation = 'designer'}
    [PSCustomObject] @{Name ='Bob'; age = 40; occupation = 'Manager'}
)

```

Remember, you can use the built-in PowerShell help system to troubleshoot:

```PowerShell
Get-Help *PSCustomObject* 

```

Would have shown you exactly where you were going wrong in its final example.

---

<div class="post-metadata">

**Author:** ![alan.revera](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alan.revera](https://forums.powershell.org/u/alan.revera)\
**Post date:** [September 19, 2026, 8:38am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/3 "2026-09-19T08:38:18Z")

</div>

```auto

```

Thanks for the reply. I read it wrong without my glasses. Sorry. I’ll try again.

---

<div class="post-metadata">

**Author:** ![alan.revera](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alan.revera](https://forums.powershell.org/u/alan.revera)\
**Post date:** [September 19, 2026, 8:54am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/4 "2026-09-19T08:54:23Z")

</div>

I eliminated the errors. Now this is the second part that display the info as array. It doesn’t work right, it doesn’t show the info. Here is the second part following the original part above:

```auto
Foreach ($person in $people) {
Write-Output “$($person.Name) age$($person.age) work as $($person.occupation)”
}

I double check the typing this time.
Thanks

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 19, 2026, 9:16am UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/5 "2026-09-19T09:16:06Z")

</div>

Well, the formatting is a bit off - your spaces are badly placed - but the code is correct and works for me. I am assuming you formatted it after copy/pasting and you don’t actually have curly quote marks in your code?

```PowerShell
foreach ($person in $people) {
    Write-Output "$($person.Name) age$($person.age) work as $($person.occupation)"
}

```

```PowerShell
#output
John age30 work as engineer
Alice age25 work as designer
Bob age40 work as Manager

```

For the best help, when something doesn’t work, it really helps us if you can post the actual error text, formatted using the \</\> button. Just saying “It doesn’t work right, it doesn’t show the info.” isn’t very helpful.

---

<div class="post-metadata">

**Author:** ![alan.revera](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alan.revera](https://forums.powershell.org/u/alan.revera)\
**Post date:** [September 19, 2026, 6:11pm UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/6 "2026-09-19T18:11:00Z")

</div>

Sorry, I really don’t see it. The only thing I see in your is you put a few spaces in front of the Write-Output. I tried that and it still comes out wrong. This is how it looks,

```auto
PS C:\Users\yungm> Foreach ($person in $people) {
>> Write-Output “$($person.Name) age$($person.age) work as $($person.occupation)”
>> }
   age work as
   age work as
   age work as

```

thanks

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 19, 2026, 7:42pm UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/7 "2026-09-19T19:42:42Z")

</div>

I think you’re missing the `@` before the braces. i.e.

You have:

```PowerShell
[PSCustomObject] { Name = 'John'; age = 30; occupation = 'engineer' }

```

Instead of:

```PowerShell
[PSCustomObject] @{ Name = 'John'; age = 30; occupation = 'engineer' }

```

The type accelerator requires a hashtable `@{}`

```PowerShell
$people = @(
    [PSCustomObject] @{ Name = 'John'; age = 30; occupation = 'engineer' }
    [PSCustomObject] @{ Name = 'Alice'; age = 25; occupation = 'designer' }
    [PSCustomObject] @{ Name = 'Bob'; age = 40; occupation = 'Manager' }
)

foreach ($person in $people) {
    Write-Output "$($person.Name) age$($person.age) work as $($person.occupation)"
}

```

---

<div class="post-metadata">

**Author:** ![alan.revera](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@alan.revera](https://forums.powershell.org/u/alan.revera)\
**Post date:** [September 19, 2026, 10:49pm UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/8 "2026-09-19T22:49:40Z")

</div>

Thanks

I got it working.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 19, 2026, 10:50pm UTC](https://forums.powershell.org/t/custom-object-array-copied-from-the-book-doesnt-work/26560/9 "2026-10-19T22:50:18Z")

</div>

Closed on October 19, 2026, 10:50pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
