# array or hash table splitting

**URL:** <https://forums.powershell.org/t/array-or-hash-table-splitting/588>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/array-or-hash-table-splitting/588 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/array-or-hash-table-splitting/588/1 "2011-12-31T18:30:00Z")

</div>

by veroli at 2013-02-11 04:08:48

> Hello,  
>   
> am having trouble splitting an array into multiple arrays  
> I have a text file with server names that is split into sections these sections are defined like an INI file   
> for example  
>   
> [server group 1]  
> server1  
> server2  
> server3  
> [server group 2]  
> server8  
> server9  
> server10  
> [server group 3]  
> server13  
> server14  
> server15  
>   
> I want to read this file in and then split these sections into three seperate arrays so i can use them   
> Its probably a really easy question but I can’t seem to split them how i want after reading in from get-content.  
> Should i split based on the and use a wildcard between them or is there a better way of handling this.  
> thanks  
>   
> Dan

by coderaven at 2013-02-11 07:48:09

> Someone may be better at this, but I think putting the data in a hash table would be best. Take the following as an example.  
>   
> `$FileName = \path\to\File.txt$Key = "Server Group X"$ResultContent = @{}$ArrContent = Get-Content -Path $FileNameforeach ($Line in $ArrContent){ $HeaderFound = $False If ($Line.StartsWith("[") { $Key = $Line; $HeaderFound = $True } if (!$HeaderFound) {$ResultContent.Add($Key, $Line)}}`  
>   
> It may be a little long for something so simple but it should work.

by mjolinor at 2013-02-11 20:33:29

> If you really want variables:  
> [code2=powershell]get-content serverlist.txt |  
> foreach {  
> if ($\_ -match '[.+]')  
> {  
> $var = $_.trim('[]').replace(' ','_')  
> New-Variable $var  
> }  
> else {set-variable -Name $var -value ((Get-Variable -Name $var).value += @($_))}  
> }_  
>   
> _get-variable server_\*[/code2]  
>   
> Name Value   
> ---- -----   
> server\_group\_1 {server1, server2, server3}   
> server\_group\_2 {server8, server9, server10}   
> server\_group\_3 {server13, server14, server15}

by veroli at 2013-02-12 00:54:55

> thanks guys i will work from this much appreciated,  
> i am also looking to see if it is more viable in an xml file might be easier to split it

---

<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, 9:03pm UTC](https://forums.powershell.org/t/array-or-hash-table-splitting/588/2 "2024-05-16T21:03:47Z")

</div>


