Powershell将循环输出移动到Array中

时间:2017-11-16 16:09:26

标签: powershell

这似乎是一个简单的答案,但对于我的生活,我无法弄明白。我想要做的是从文本文件中获取一堆电子邮件地址,并删除超过@的电子邮件位(例如,如果电子邮件地址是adam@home.com,我对adam位感兴趣)。

下面的脚本输出我想要的值但是我希望将输出写入数组而不是控制台。这样做的最佳方式是什么?

下面的代码,谢谢。

# 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)

1 个答案:

答案 0 :(得分:2)

您的电子邮件解析并没有多大意义。这是您的问题的单行内容:

$Arr = @(Get-Content -Path 'C:\temp\test.txt') -replace '@.+'