1

It seems like a simple answer but for the life of me I cant figure it out. What I am trying to do is get a bunch of email addresses out of a text file and strip out the bit of the email past the @ (e.g if the email address was [email protected] I am interested in the adam bit).

The script below outputs the values that I want however I would like to have the output written into a array rather than to the console. What would be the best way of doing this?

Code below, thanks.

# Import the text file into a variable 
$import = Get-Content c:\\temp\test.txt
# Variable to hold total number of emails
$totalEmails = $import.Count
#Variable to run loop
$start = 0
#Used as a separator to look at the first part of the email address
$separator = "@"
# Will increment by two to show the first part of the array in parts which     holds the text before the @ 
$even = 0
# Username after @
$userName


#Strip out the text before the @
do {

    $parts = $import.split($separator)
    $even+=2

    # This line here is whats not working, I would like to have the output of     each echo written into the array $userName 
    $userName = echo $parts[$even]


    $start++

} while ($totalEmails -gt $start)
8
  • This replaces your whole script: $array = Get-Content "C:/temp/test.txt" | % {$_ | Split-String -Separator "@" | Select -First 1} Get a look at about_Pipelines Commented Nov 16, 2017 at 16:16
  • Hello, thanks for your response! I tried to run this but it errors on the Split-String statement? Commented Nov 16, 2017 at 16:24
  • Sure? What's the error? Commented Nov 16, 2017 at 16:43
  • @Clijsters Bet its split-string : The term 'split-string' is not recognized as the name of a cmdlet, function, script file, Commented Nov 16, 2017 at 16:44
  • There isn't a Split-String cmdlet. Commented Nov 16, 2017 at 17:09

1 Answer 1

2

Your email parsing doesn't make a whole lot of sense. Here's a one-liner for your question:

$Arr = @(Get-Content -Path 'C:\temp\test.txt') -replace '@.+'
Sign up to request clarification or add additional context in comments.

4 Comments

This done the trick, thank you very much! Just so I can understand what exactly I have done, the statement of "-replace '@.+'" is that just cutting off the rest of the text after the @?
Yes, it is replacing the @ character and every character after it with the empty string ''
It doesn't really apply in this case since the regex is simple, but I rarely pass on an opportunity to use one of my favorite quotes: "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." - Jamie Zawinski
@MrI The regex translates to: literal @ character, wildcard character 1+. So, @ and everything until end of line and by omitting the second argument, it gets deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.