'powershell -split('') specify a new line

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



Solution 1:[1]

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

Solution 2:[2]

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.

Solution 3:[3]

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)

Solution 4:[4]

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.

Solution 5:[5]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 aphoria
Solution 3 marko.ristin
Solution 4 RcINS
Solution 5