# Help converting string to psobject..

**URL:** <https://forums.powershell.org/t/help-converting-string-to-psobject/12306>\
**Category:** PowerShell Help\
**Created:** [March 31, 2019, 1:53pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306 "2019-03-31T13:53:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![poratfreak](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@poratfreak](https://forums.powershell.org/u/poratfreak)\
**Post date:** [March 31, 2019, 1:53pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/1 "2019-03-31T13:53:17Z")

</div>

I cannot convert this simple string to a psobject using ConvertFrom-String.

What am I missing?

[pre]

$test\_string = @’  
0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING  
18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING  
18a9-05fe-e65a 1 LEARNED GigabitEthernet1/0/33 AGING  
5cb9-01af-b4f6 1 LEARNED GigabitEthernet1/0/17 AGING  
'@

$mac\_list = $test\_string | ConvertFrom-String -PropertyNames MAC,VLAN,TYPE,PORT,AGE  
$mac\_list | Format-Table -AutoSize

[/pre]

Result:

MAC VLAN TYPE PORT AGE P6 P7 P8 P9 P10

* * *

0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING 18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [March 31, 2019, 2:37pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/2 "2019-03-31T14:37:48Z")

</div>

That’s because $test\_string is one big string. You’d need to break up each line and pass them individually. A “here string” is still a single string.

[pre]$test\_string = @"  
0025-b3f9-3053 1 LEARNED GigabitEthernet1/0/46 AGING  
18a9-05fd-2e15 1 LEARNED GigabitEthernet1/0/40 AGING  
18a9-05fe-e65a 1 LEARNED GigabitEthernet1/0/33 AGING  
5cb9-01af-b4f6 1 LEARNED GigabitEthernet1/0/17 AGING  
"@

$Entries = $test\_string.Split([Environment]::NewLine)  
foreach($Entry in $Entries)  
{  
$mac\_list = $Entry | ConvertFrom-String -PropertyNames MAC,VLAN,TYPE,PORT,AGE  
$mac\_list | Format-Table -AutoSize  
}[/pre]

---

<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:** [March 31, 2019, 3:34pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/3 "2019-03-31T15:34:23Z")

</div>

Could it be that what you’re actually looking for?

```
$test_string -split “`r`n” |
ConvertFrom-Csv -Delimiter " " -Header MAC, VLAN, TYPE, PORT, AGE |
Format-Table -AutoSize
```

---

<div class="post-metadata">

**Author:** ![poratfreak](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@poratfreak](https://forums.powershell.org/u/poratfreak)\
**Post date:** [March 31, 2019, 4:15pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/4 "2019-03-31T16:15:22Z")

</div>

Thanks for the tips, I had to modify it a bit to make it work:

[pre]

$final\_list = ($test\_string -split "`r`n" -replace '\s{1,}', ',' | ConvertFrom-Csv -Delimiter "," -Header MAC, VLAN, TYPE, PORT, AGE)

$final\_list | Format-table -AutoSize

[/pre]

I still don't understand why ConvertFrom-String is not working in the first place ... Weird

---

<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:** [March 31, 2019, 4:46pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/5 "2019-03-31T16:46:40Z")

</div>

Actually it does not matter what convert you use …

```
$test_string -split "`r`n" | 
    ConvertFrom-String -Delimiter ' ' -PropertyNames MAC, VLAN, TYPE, PORT, AGE |
        Format-Table -AutoSize
```

… works as well. You just have to split your looooong input string into meaningful little pieces. 😉  
BTW: you can simplify your **-replace ‘\s{1,}’,‘,’** a tiny little bit to **-replace ‘\s+’,‘,’**

---

<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:33pm UTC](https://forums.powershell.org/t/help-converting-string-to-psobject/12306/6 "2024-05-16T20:33:05Z")

</div>


