Q:

How fix foreach loop

My foreach loop add all content logs to single line. How fix it to add content to index?
$FilterHashTable = @{logname='Application'; providername='Microsoft-Windows-   SoftwareRestrictionPolicies'; StartTime=$StartTime};
$test = Invoke-Command -ScriptBlock {param ($FilterHashTable) Get-WinEvent -FilterHashtable $FilterHashTable} -ArgumentList $FilterHashTable -ComputerName 491-517 | 
Select-Object PSComputerName, TimeCreated, Message 

foreach ($id in $test)
{
$PSComputerName = $test.PSComputerName
$TimeCreated = $test.TimeCreated
$Message = $test.Message

Add-Content $logFile  "$PSComputerName, $TimeCreated, $Message"
}

A:

Take a look there: http://ss64.com/ps/foreach.html
$id is the the next element in your list, refer to $id instead of $test
Try this
foreach ($id in $test)
{

$PSComputerName = $id.PSComputerName
$TimeCreated = $id.TimeCreated
$Message = $id.Message

Add-Content $logFile  "$PSComputerName, $TimeCreated, $Message"
}

