Powershell: How to I test if a line of text contains line feed or carriage return? Powershell: How to I test if a line of text contains line feed or carriage return? powershell powershell

Powershell: How to I test if a line of text contains line feed or carriage return?


Get-Content converts your file into a sting array based on those characters. By the time you start to test the contents those characters have been stripped out. If you are just looking for the presence of newlines (your condition does not seem to care what it encounters) testing the amount of lines returned from Get-Content would be enough.

If((Get-Content $masterFilename).Count -gt 1){"you betcha, there's a carriage return or line feed"}

I'm sure I will add to this once you see it. I think you might need to revise your question to be a little more specific.

Get-Content has a -Raw switch which would preserve the new line delimiters which is closer to where you want to be going. I'm not sure the string method contains supports wildcards. I do know that it does not support regex.


If you were just curious about one line then as mentioned above -Raw would be the place to start. Consider the following text file represented in hex. It is just the word hello followed by a newline.

68 65 6c 6c 6f 0d 0a

Now I will read that text in as one string and test if the end of the string has a carriage return and line feed. Note that I am not escaping with backticks. I am using regex special characters. (It would work either way. Just something to be aware of.)

(Get-Content c:\temp\test.txt -Raw) -match "\r\n$"True

Note that most of the PowerShell cmdlets that write to file leave a trailing newline. So no matter how many lines you have the above code snippet would be true for something like Set-Content.