32
Get-Content $user| Foreach-Object{
   $user = $_.Split('=')
   New-Variable -Name $user[0] -Value $user[1]}

Im trying to work on a script and have it split a text file into an array, splitting the file based on each new line

What should I change the "=" sign to

colbyt
  • 437
  • 1
  • 5
  • 6

6 Answers6

63

It depends on the exact encoding of the textfile, but [Environment]::NewLine usually does the trick.

"This is `r`na string.".Split([Environment]::NewLine)

Output:

This is

a string.

knrdk
  • 528
  • 5
  • 13
Ryan Ries
  • 2,224
  • 21
  • 31
  • 14
    This will contain empty elements between each line in the original string. – sinned Mar 12 '18 at 09:31
  • Thank you * 1000! This helped me with dealing with the Message property within Windows Event logs, in particular Sysmon event logs. – Paul Masek Nov 21 '19 at 15:18
  • Struggled across trying out loads of code snippets, but this small pearl worked like a charm! Thank you, @Ryan – Yash Gupta Jun 27 '20 at 17:53
24

The problem with the String.Split method is that it splits on each character in the given string. Hence, if the text file has CRLF line separators, you will get empty elements.

Better solution, using the -Split operator.

"This is `r`na string." -Split "`r`n" #[Environment]::NewLine, if you prefer
LCC
  • 630
  • 8
  • 10
15

You can use the String.Split method to split on CRLF and not end up with the empty elements by using the Split(String[], StringSplitOptions) method overload.

There are a couple different ways you can use this method to do it.

Option 1

$input.Split([string[]]"`r`n", [StringSplitOptions]::None)

This will split on the combined CRLF (Carriage Return and Line Feed) string represented by `r`n. The [StringSplitOptions]::None option will allow the Split method to return empty elements in the array, but there should not be any if all the lines end with a CRLF.

Option 2

$input.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries)

This will split on either a Carriage Return or a Line Feed. So the array will end up with empty elements interspersed with the actual strings. The [StringSplitOptions]::RemoveEmptyEntries option instructs the Split method to not include empty elements.

aphoria
  • 19,178
  • 7
  • 59
  • 70
5

The answers given so far consider only Windows as the running environment. If your script needs to run in a variety of environments (Linux, Mac and Windows), consider using the following snippet:

$lines = $input.Split(
    @("`r`n", "`r", "`n"), 
   [StringSplitOptions]::None)
marko.ristin
  • 553
  • 6
  • 6
0

There is a simple and unusual way to do this.

$lines = [string[]]$input

This will split $input like:

$input.Split(@("`r`n", "`n"))

This is undocumented at least in docs for Conversions.
Beware, this will not remove empty entries. And it doesn't work for Carriage Return (\r) line ending at least on Windows.
Experimented in Powershell 7.2.

RcINS
  • 21
  • 3
-1

This article also explains a lot about how it works with carriage return and line ends. https://virot.eu/powershell-and-newlines/

having some issues with additional empty lines and such i found the solution to understanding the issue. Excerpt from virot.eu:

So what makes up a new line. Here comes the tricky part, it depends. To understand this we need to go to the line feed the character.

Line feed is the ASCII character 10. It in most programming languages escaped by writing \n, but in powershell it is `n. But Windows is not content with just one character, Windows also uses carriage return which is ASCII character 13. Escaped \r. So what is the difference? Line feed advances the pointer down one row and carriage return returns it to the left side again. If you store a file in Windows by default are linebreaks are stored as first a carriage return and then a line feed (\r\n). When we aren’t using any parameters for the split() command it will split on all white-space characters, that is both carriage return, linefeed, tabs and a few more. This is why we are getting 5 results when there is both carriage return and line feeds.

Paul Fijma
  • 447
  • 4
  • 9
  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – 4b0 Jun 30 '21 at 06:10
  • Nice edit. To make it even better please use https://stackoverflow.com/editing-help and formatting to make obvious which parts you quote and which you phrased yourself. It can also indicate the small code parts you have. – Yunnosch Jun 30 '21 at 06:16
  • 1
    thanks for the pointers :-) i have added the suggestions. – Paul Fijma Jun 30 '21 at 06:29