powershellwrite-host

redirecting test-path output to text file


The txt file is just a bunch of UNC paths, i am trying to get a list of UNC paths from this text file put into another text file after the test-path is validated. it shows the validated paths on screen but the text file does not populate.

$cfgs = Get-Content .\cfgpath.txt
$cfgs | % {
  if (Test-Path $_) { write-host "$_" | Out-File -FilePath c:\temp\1.txt -Append }
}

Solution

  • Write-Host only writes to the console. I believe what you want there is Write-Output.

    $cfgs = Get-Content .\cfgpath.txt
    $cfgs | % {
      if (Test-Path $_) { write-output "$_" | Out-File -FilePath c:\temp\1.txt -Append }
    }
    

    Additionally you can just omit the Write-Output and that works too.

    $cfgs = Get-Content .\cfgpath.txt
    $cfgs | % {
      if (Test-Path $_) { "$_" | Out-File -FilePath c:\temp\1.txt -Append }
    }