Question:
I have a file like this:
1 2 3 4 5 |
line one email1 line two email1 line three email2 line four email1 |
If I want to extract only the lines that contain “email1”, I do this:
1 2 |
$text = Get-Content -Path $path | Where-Object { $_ -like *email1* } |
$text is now an array with 3 elements containing those lines, and I iterate through it like this:
1 2 3 4 5 |
for ($i = 0; $i -lt $text.Length; $i++) { #do stuff here } |
However, if I want to get the lines containing “email2”.
1 2 |
$text = Get-Content -Path $path | Where-Object { $_ -like *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[]]
.
1 2 |
[String[]]$logText = Get-Content -Path $path | Where-Object { $_ -like *email1* } |