Powershell Get-Content with Where-Object

Question:

I have a file like this:

If I want to extract only the lines that contain “email1”, I do this:

$text is now an array with 3 elements containing those lines, and I iterate through it like this:

However, if I want to get the lines containing “email2”.

returns a string, rather than an array of one element.
And when I iterate through it, it iterates through each char in the string.

How can I make it an array with one element rather than a string?

Answer:

Worked it out.

I need to declare $text as type [String[]].

Source:

Powershell Get-Content with Where-Object by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply