# Hashtable - export : to CSV

**URL:** <https://forums.powershell.org/t/hashtable-export-to-csv/4426>\
**Category:** PowerShell Help\
**Created:** [June 25, 2015, 4:03am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426 "2015-06-25T04:03:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![windows-liveuser32](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@windows-liveuser32](https://forums.powershell.org/u/windows-liveuser32)\
**Post date:** [June 25, 2015, 4:03am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/1 "2015-06-25T04:03:34Z")

</div>

Hi everyone,  
I am experimenting a little bit with Hashtables in PowerShell v2 and v3 and v4. I am asking for your help in regards of exporting multiple values to a CSV-file. Here’s what I’m trying:  
Get the status of the Windows Defender Service via WMI:

```
$WinDefend = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name LIKE 'WinDef%'"
```

Store multiple values regarding the Service in a Hashtable:

```
$SystemProps=[ordered]@{
#$SystemProps=@{
'WindowsDefender'=$WinDefend.ProcessID,$WinDefend.State,$WinDefend.StartMode;
}
```

If I try exporting the Hashtable to CSV I always end up with a “System.Object” in the column where those multiple values are stored. Below is what I’ve tried so far

```
$SystemProps.GetEnumerator() | select name,value | Export-CSV C:\temp\props.csv
new-object psobject -property $SystemProps | Export-Csv "C:\temp\props.csv" -NoTypeInformation
```

Writing the values out to a txt-file works fine but I’m curious of how to do that with a CSV

```
$SystemProps.GetEnumerator() | Out-String | out-file C:\temp\props.txt
```

Thanks a lot,  
Steven

---

<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:** [June 25, 2015, 5:27am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/2 "2015-06-25T05:27:12Z")

</div>

You can turn it into an object using pscustomobject, and then successfully pipe that to a Csv. Notice, I’ve also created a separate keys for process, state, and startmode. I’m not sure if this will work (for you), so if not, take a look at the second option to leave the process, state, and startmode as a single value for the one, WindowsDefender, key.

```
$WinDefend = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name LIKE 'WinDef%'"

$SystemProps = [pscustomobject][ordered]@{
    WindowsDefenderProcess = $WinDefend.ProcessID
    WindowsDefenderState = $WinDefend.State
    WindowsDefenderStartMode = $WinDefend.StartMode;
}

$SystemProps | Export-Csv -Path C:\temp\props1.csv -NoTypeInformation
```

```
$WinDefend = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name LIKE 'WinDef%'"

$SystemProps = [pscustomobject][ordered]@{
    WindowsDefender = $WinDefend.ProcessID,$WinDefend.State,$WinDefend.StartMode -join ','
}

$SystemProps | Export-Csv -Path C:\temp\props2.csv -NoTypeInformation
```

Box Prox wrote about this in 2014, and I’ve referenced the post numerous times when I hit the System.Object in my exported, Csv files. Here’s the link: [http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/](http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/).

Most recently, I used it to a fix a problem with a DHCP/DNS function I wrote: [http://tommymaynard.com/script-sharing-find-dns-servers-being-used-by-dhcp-scopes-2015](http://tommymaynard.com/script-sharing-find-dns-servers-being-used-by-dhcp-scopes-2015). I even gave him credit at the bottom of that post.

Anyway, let me know if this helps, and if you can’t scroll left-right in the script windows, use the double-sided arrows to open it in a separate window.

---

<div class="post-metadata">

**Author:** ![windows-liveuser32](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@windows-liveuser32](https://forums.powershell.org/u/windows-liveuser32)\
**Post date:** [June 25, 2015, 6:06am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/3 "2015-06-25T06:06:49Z")

</div>

Hey sir,  
thank you very much! Both approaches work with PowerShell v3. Unfortunately I didn’t get them to work with PowerShell v2. The output I get is:

```
"IsReadOnly","IsFixedSize","IsSynchronized","Keys","Values","SyncRoot","Count"
"False","False","False","System.Collections.Hashtable+KeyCollection","System.Collections.Hashtable+ValueCollection","System.Object","6"
```

Is this exspected behavior or am I doing something wrong?  
Thanks and best regards,  
Steven

---

<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:** [June 25, 2015, 6:13am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/4 "2015-06-25T06:13:53Z")

</div>

That’s expected behavior prior to PowerShell 3.0, when creating a custom object with this option. There’s different ways to create custom objects in the earlier versions of PowerShell. Here’s how to do it in the different versions: [http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/07/a-powershell-object-lesson-part-3.aspx](http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/07/a-powershell-object-lesson-part-3.aspx). I choose PowerShell 3.0, since I _think_ that’s when we were able to order our hash tables, such as what you did in your example.

---

<div class="post-metadata">

**Author:** ![windows-liveuser32](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@windows-liveuser32](https://forums.powershell.org/u/windows-liveuser32)\
**Post date:** [June 25, 2015, 6:17am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/5 "2015-06-25T06:17:59Z")

</div>

Thank you very much sir! Exactly what I needed!

---

<div class="post-metadata">

**Author:** ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)\
**Post date:** [June 25, 2015, 7:11am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/6 "2015-06-25T07:11:28Z")

</div>

Stephen,

No need to use a hashtable as an interim step.

```
$WinDefend = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name LIKE 'WinDef%'"
$WinDefend | Select ProcessID, State, StartMode | Export-Csv "C:\temp\props.csv" -NoTypeInformation
```

If you need to change the column header, you do it like this:

```
$WinDefend = Get-WmiObject -Namespace root\cimv2 -Class Win32_Service -Filter "Name LIKE 'WinDef%'"
$WinDefend | Select @{ Label = 'WindowsDefenderProcess'; Expression = { $_.ProcessID} }, State, StartMode | Export-Csv "C:\temp\props.csv" -NoTypeInformation
```

---

<div class="post-metadata">

**Author:** ![windows-liveuser32](https://avatars.discourse-cdn.com/v4/letter/w/13edae/32.png) [@windows-liveuser32](https://forums.powershell.org/u/windows-liveuser32)\
**Post date:** [June 25, 2015, 7:15am UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/7 "2015-06-25T07:15:07Z")

</div>

Hi Tim,  
thanks, but my goal was just to play around with Hashtables, therefore I used it 🙂 With Tommy’s help I now figured out that for outputting stuff to a CSV-File a custom PSObject is better choice than a Hashtable.So I learned two things in one catch. Looks like a good day from my perspective 🙂

Greetings,  
Steven

---

<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:45pm UTC](https://forums.powershell.org/t/hashtable-export-to-csv/4426/8 "2024-05-16T20:45:06Z")

</div>

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


