# System.String export to csv

**URL:** <https://forums.powershell.org/t/system-string-export-to-csv/4824>\
**Category:** PowerShell Help\
**Created:** [September 4, 2015, 2:31am UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824 "2015-09-04T02:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [September 4, 2015, 2:31am UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824/1 "2015-09-04T02:31:03Z")

</div>

I’m trying to get all permanent routes off remote servers and I started with the following  
$Computers = “.”

$PermRoutes = get-wmiobject -class “Win32\_IP4PersistedRouteTable” -namespace “root\CIMV2” -computername $Computers

$routes = foreach ($object in $PermRoutes) {

```
  $object.Description

```

}  
$routes | Export-CSV .\routes.csv -NoTypeInformation

which displays a nice list of routes  
like 10.0.0.2,255.255.0.0,169.30.1.1,1  
however my csv only contains the $routes.Length property. I’m really bad with tables and arrays and not sure why instead of coma separated values I get only one value which is Length. Could someone explain what’s going on in the script so I understand why I’m not getting what I expect?

---

<div class="post-metadata">

**Author:** ![christopher-wolfende](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@christopher-wolfende](https://forums.powershell.org/u/christopher-wolfende)\
**Post date:** [September 4, 2015, 3:23am UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824/2 "2015-09-04T03:23:37Z")

</div>

Take a look at:

[http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/23/use-powershell-to-work-with-csv-formatted-text.aspx](http://blogs.technet.com/b/heyscriptingguy/archive/2011/09/23/use-powershell-to-work-with-csv-formatted-text.aspx)

If you just want to export the data you can just use Out-File:

```
$routes | Out-File .\routes.csv
```

However, if you plan on building on this and pulling more data it may make sense change to a custom object:

```
$PermRoutes = get-wmiobject -class "Win32_IP4PersistedRouteTable" -namespace "root\CIMV2" -computername $Computers

$routes = foreach ($object in $PermRoutes) {
    [PSCustomObject]@{

                    Description = $object.Description
                    # You can add in addtional Properties
                    #ComputerName = $object.PSComputerName
                    #RouteName = $object.Name
}

$routes | Export-CSV .\routes.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![christopher-wolfende](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@christopher-wolfende](https://forums.powershell.org/u/christopher-wolfende)\
**Post date:** [September 4, 2015, 3:36am UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824/3 "2015-09-04T03:36:40Z")

</div>

An even simpler approach would be to just use a select statement:

```
$PermRoutes = get-wmiobject -class "Win32_IP4PersistedRouteTable" -namespace "root\CIMV2" -computername $Computers

$PermRoutes | Select-Object -Property Description | Export-CSV .\routes.csv -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [September 5, 2015, 8:20pm UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824/4 "2015-09-05T20:20:50Z")

</div>

Thanks Chris for great explanation. I see you use the PSCustomObject in a quite different way than the Scripting Guy does and to me less confusing. The 2-liner makes things even easier.

---

<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:44pm UTC](https://forums.powershell.org/t/system-string-export-to-csv/4824/5 "2024-05-16T20:44:49Z")

</div>

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


