# Split function

**URL:** <https://forums.powershell.org/t/split-function/448>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/split-function/448 "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/split-function/448/1 "2011-12-31T18:30:00Z")

</div>

by Porthos at 2013-01-02 13:39:06

> Hi Everyone,  
>   
> I have a question about using the -split function  
>   
> I currently have a text file that has the following info among a bunch of other things…  
> ` ALIAS : Domain1\D-Servers-Local Administrators ALIAS : Domain1\D-Server Administrators GROUP : Domain2\Enterprise Admins GROUP : Domain2\U-Admin GROUP : Domain2\U-Power Users GROUP : Domain1\Domain Admins GROUP : Domain4\Domain Users GROUP : Domain1\OUAdmins GROUP : Domain1\SMSAdmins GROUP : Domain3\Operators USER : Domain2\User-admin USER : Domain1\User1 USER : Domain1\User2`  
>   
> So basically I am trying to use the following line to fetch me that list from the text file and pull out just the group,alias,and user names  
>   
> `Get-Content $Path\LocalGroups\Groups.txt | Sort-Object -Unique | Select-String -SimpleMatch "Domain1&quot;,"Domain2&quot;,"Domain3&quot;,"Domain4&quot;,"Domain5&quot; | %{$-split(": ")}`  
>   
> _However I want to just get the names after the colon. When I run that line of code I get something like the following:  
> `ALIASDomain1\D-Servers-Local AdministratorsALIAS Domain1\D-Server AdministratorsGROUP Domain2\Enterprise Adminsetc...`_  
>   
> _What do I need to just get the second half of the split then write it to a text file?_  
>   
> _Thanks!_

_by DonJ at 2013-01-02 13:49:38_

> _-Split produces an array. Because you’re feeding it one line at a time, you’re getting several arrays._  
>   
> _You might try:  
> `ForEach { ($`_` -split(": "))[1] }`  
> (Sorry, can’t abide the % alias)

by Porthos at 2013-01-02 13:59:12

> DonJ,  
>   
> That seems to just return the info from the text file. It doesnt split the information at all

by DonJ at 2013-01-02 14:11:32

> Sorry, I don’t have a copy of the shell I can play with. The general idea is that -split always creates an array. Since your string is being split into two parts, it’ll be a two-element array. [0] will be the bit before the : and [1] will be the bit after. That’s what you’ll need to work with.

by Porthos at 2013-01-02 17:15:58

> Hey DonJ,  
>   
> I understand what you are saying however, I am still getting the same results using 0 or 1…here is what I have so far…  
>   
> `Get-Content $Path\LocalGroups\Groups.txt | Sort-Object -Unique | Select-String -SimpleMatch "Domain1&quot;,"Domain2&quot;,"Domain3&quot;,"Domain4&quot;,"Domain5&quot; | ForEach{($_ -split(":"))[1]}`  
>   
> I am fairly new to this so not sure what I may be doing wrong, am I possibly trying to string too many things together? Sorting, selecting and splitting?

by DonJ at 2013-01-02 17:28:23

> Yeah possibly. I’d probably try to write this over several lines instead of in a one liner. Easier to debug.

by Jules at 2013-01-02 23:56:04

> Instead of doing   
> `ForEach { ($_ -Split(": "))[1] }`  
> try  
> `ForEach { $_.Split(": ")[1] }`

by Porthos at 2013-01-03 06:31:18

> Unfortunately still get the same result.  
>   
> If I change it to 0, it returns the left side of the ":" on one line and the right side on the next line.   
> ` ALIAS Domain1\D-Servers-Local Administrators ALIAS Domain1\D-Server Administrators GROUP Domain2\Enterprise Admins GROUP Domain2\U-Admin GROUP Domain2\U-Power Users GROUP Domain1\Domain Admins GROUP Domain4\Domain Users GROUP Domain1\OUAdmins GROUP Domain1\SMSAdmins GROUP Domain3\Operators`  
>   
> If I change it to 1, it returns the entire line as if its not splitting it at all.  
> ` ALIAS : Domain1\D-Servers-Local Administrators ALIAS : Domain1\D-Server Administrators GROUP : Domain2\Enterprise Admins GROUP : Domain2\U-Admin GROUP : Domain2\U-Power Users GROUP : Domain1\Domain Admins GROUP : Domain4\Domain Users GROUP : Domain1\OUAdmins GROUP : Domain1\SMSAdmins GROUP : Domain3\Operators`

by Klaas at 2013-01-03 07:54:19

> Can you post the exact code you’re using now, please?

---

<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:58pm UTC](https://forums.powershell.org/t/split-function/448/2 "2024-05-16T20:58:08Z")

</div>

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


