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)
$array = Get-Content "C:/temp/test.txt" | % {$_ | Split-String -Separator "@" | Select -First 1}Get a look at about_Pipelinessplit-string : The term 'split-string' is not recognized as the name of a cmdlet, function, script file,