I am new to Powershell and I am wondering why this function doesn't work the way I want it too. The following function uncomments line in the hosts file of the computer specified. It works great! But I want the ability to output each line to the screen using a Write-Output and get the variable and store it in another array. This Write-Output doesn't do anything and I cannot add it to the array. Why? The above Write-Output works just fine.
function UnCommentHostsFile($ip){
Write-Output "This Write-Output Works!"
$hosts = $hosts | Foreach {
#This part of the function works great!
if ($_ -match $regex + $ip){
$_.replace("#", "")
#This is where I want to add it to an array and write-output but I can't.
}
else {
$_
}
#This does not output!
Write-Output $_
}
}
Any help would be greatly appreciated! Thanks! :)