# Write some text in foreach-object

**URL:** <https://forums.powershell.org/t/write-some-text-in-foreach-object/20229>\
**Category:** PowerShell Help\
**Created:** [August 22, 2022, 5:42pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229 "2022-08-22T17:42:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![deuval](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@deuval](https://forums.powershell.org/u/deuval)\
**Post date:** [August 22, 2022, 5:42pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/1 "2022-08-22T17:42:11Z")

</div>

Hello,

```auto
Clear-Host
#variable

$table = New-Object System.Data.Datatable

#adding columns
[void]$table.Columns.Add("Sites")
[void]$table.Columns.Add("Processus")
[void]$table.Columns.Add("Room")

#adding rows
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("CHALON","Kitchen","209")

#$table | Where-Object {($_.Sites -eq 'MACON') -and ($_.Processus -in 'Direction')}

$table | ForEach-Object {
    $_.Processus
}

```

This is works. But if a do this :

```auto
$table | ForEach-Object {
    write-host " you are in : $_.Processus"
}

```

i have this :  
you are in : System.Data.DataRow.Processus  
you are in : System.Data.DataRow.Processus  
you are in : System.Data.DataRow.Processus

How can i show processus with some text ?

---

<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:** [August 22, 2022, 5:47pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/2 "2022-08-22T17:47:39Z")

</div>

You need to use the subexpression operator when accessing properties of objects in double quotes:

```auto
$table | ForEach-Object {
    write-host " you are in : $($_.Processus)"
}

```

> **[about Operators - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.2)**
>
> Describes the operators that are supported by PowerShell.

---

<div class="post-metadata">

**Author:** ![deuval](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@deuval](https://forums.powershell.org/u/deuval)\
**Post date:** [August 22, 2022, 7:51pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/3 "2022-08-22T19:51:17Z")

</div>

Thanks ! I progress…

now i want only the unique item. I think that I have use select -unique in $tableProcessus, but i don’t find where, and the good syntax

```auto
Clear-Host
#variable

$table = New-Object System.Data.Datatable

#adding columns
[void]$table.Columns.Add("Sites")
[void]$table.Columns.Add("Processus")
[void]$table.Columns.Add("Room")

#adding rows
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Communication","101")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("MACON","Direction","105")
[void]$table.Rows.Add("CHALON","Kitchen","209")

$tableProcessus=$table | Where-Object {($_.Sites -eq 'MACON')}

$i=1
$tableProcessus | ForEach-Object {
    
    write-host "$i - $($_.Processus)"
    $i++
}

$choixProcessus=Read-Host "choose your processus : "
$choixProcessus=$choixProcessus-1

write-host "valeur processus choisi : $($tableProcessus.Processus[$choixProcessus])"

$tableBureau=$table | Where-Object {($_.Sites -eq 'MACON') -and ($_.Processus -eq $($tableProcessus.Processus[$choixProcessus]))}

$tableBureau | ForEach-Object {   
    write-host "room is : $($_.Room)"
}

```

---

<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 22, 2022, 8:09pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/4 "2022-08-22T20:09:17Z")

</div>

Just to mention it at least once … you know there is an easier way of creating data in table format, don’t you?

```auto
$table = @'
Sites,Processus,Room
MACON,Communication,101
MACON,Communication,101
MACON,Communication,101
MACON,Communication,101
MACON,Direction,105
MACON,Direction,105
MACON,Direction,105
CHALON,Kitchen,209
'@ |
    ConvertFrom-Csv

```

And because PowerShell is so forgiving you don’t even need to use double quotes for the strings. 😉

> [@deuval](#):
>
> now i want only the unique item. I think that I have use select -unique in $tableProcessus, but i don’t find where, and the good syntax

I’m not sure if I got what you mean … try this:

```auto
$table | 
    Select-Object -unique Sites,Processus,Room

```

---

<div class="post-metadata">

**Author:** ![deuval](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@deuval](https://forums.powershell.org/u/deuval)\
**Post date:** [August 23, 2022, 7:04pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/5 "2022-08-23T19:04:34Z")

</div>

i don’t think is the same result.

The result of $table |  
Select-Object -unique Sites,Processus,Room  
ok i progress ! i progress …

i could do that with your help. Now i need to get the index of an object. How can I do this ?

```auto
$town="macon"

$table = @'
Sites,Processus,Room
MACON,Communication,101
MACON,info,101
MACON,Communication,102
MACON,Communication,101
louhans,Direction,105
MACON,Direction,107
MACON,Direction,105
CHALON,Kitchen,209
'@ | ConvertFrom-Csv

#$table | Select-Object -unique Sites,Processus,Room | Where-Object {($_.Sites -eq $town) }

write-host "Welcome in $town. Choose the processus available : "
write-host ""
write-host ""

$Tableprocessus = $table | Where-Object {($_.Sites -eq $town) } | sort-Object -unique Processus

$i=1

#display processus here a want display communication and info
$Tableprocessus | ForEach-Object{
    write-host "$i - $($_.Processus)"
    $i++
}

write-host ""

$choiceProcessus = Read-Host " enter a number"

write-host "choose the processus room's available "

$TableRoom = $table | Where-Object {($_.Sites -eq $town) -and ($_.Processus -eq 'communication') } | sort-Object -unique Room
#display room
$j=1
$TableRoom | ForEach-Object{
    write-host "$j - $($_.Room)"
    $j++
}  

$choiceRoom = Read-Host " enter a number"

```

---

<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 24, 2022, 9:59am UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/6 "2022-08-24T09:59:39Z")

</div>

> [@deuval](#):
>
> i don’t think …

That tends to be an unreliable source of information. 😉

> [@deuval](#):
>
> Now i need to get the index of an object. How can I do this ?

I’m not sure if I really got what you mean. If it’s what I think I’d do something like this:

```auto
$table = @'
Sites,Process,Room
MACON,Communication,101
MACON,info,101
MACON,Communication,102
MACON,Communication,103
louhans,Direction,104
MACON,Direction,107
MACON,Direction,105
CHALON,Kitchen,209
louhans,Direction,108
'@ | 
ConvertFrom-Csv

$SiteGroup =
$table | 
Group-Object -Property Sites

$SiteGroup | 
Select-Object -Property Name, 
@{Name = 'Index'; Expression = {[array]::IndexOf($SiteGroup.Name, $_.Name)}} |
Out-Host

$SelectedSiteIndex =
Read-Host -Prompt 'Please select an index of the desired site'

$ProcessGroup =
$SiteGroup[$SelectedSiteIndex].Group |
Group-Object -Property Process

$ProcessGroup | 
Select-Object -Property Name, 
@{Name = 'Index';Expression = {[array]::IndexOf($ProcessGroup.Name,$_.Name)}} |
Out-Host
    
$SelectedProcessIndex =
Read-Host -Prompt 'Please select an index of the desired process'

$ProcessGroup[$SelectedProcessIndex].Group

```

---

<div class="post-metadata">

**Author:** ![deuval](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@deuval](https://forums.powershell.org/u/deuval)\
**Post date:** [August 24, 2022, 5:23pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/7 "2022-08-24T17:23:13Z")

</div>

nice script !  
i have many thing to learn, never use group-object, $ProcessGroup |  
Select-Object -Property Name,  
@{Name = ‘Index’;Expression = {[array]::IndexOf($ProcessGroup.Name,$\_.Name)}} | . Hard to me to understand… need some time

i try to explain what i want :

the process display all site WITHOUT duplicate :  
1 MACON  
2 louhans  
3 CHALON

The user select 1

The process display all processus WITHOUT duplicate linked by chosen site MACON :  
1 communication  
2 info  
3 direction

The user select 1 again

The process display all room without duplicate linked by the chosen site MACON AND the chosen process communication :  
1 101  
2 102  
3 103

---

<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 24, 2022, 6:16pm UTC](https://forums.powershell.org/t/write-some-text-in-foreach-object/20229/8 "2022-08-24T18:16:42Z")

</div>

> [@deuval](#):
>
> Hard to me to understand… need some time

No Problem. Take the time you need. 😉

> [@deuval](#):
>
> i try to explain what i want :

Whatfor? I already showed to you how I would do it. You can use my suggestions if you like or don’t if you dislike. 🤷🏽‍♂️ And of course you are allowed to adapt it to whatever requirement you like.

> [@deuval](#):
>
> the process display all site WITHOUT duplicate

That already happens with my code suggestion

> [@deuval](#):
>
> The process display all processus WITHOUT duplicate linked by chosen site MACON :

That already happens with my code suggestion.

> [@deuval](#):
>
> The process display all room without duplicate linked by the chosen site MACON AND the chosen process communication :

That already happens with my code suggestion.

---

<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/write-some-text-in-foreach-object/20229/9 "2024-05-16T20:16:41Z")

</div>

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


