Search code examples
powershell-3.0

PowerShell : writing to text file


I am trying to get a list installed printers for a list of computer.

When I run this script, it only "writes" the last computer's information.

I am VERY new to PS and would appreciate some help.

$filePath = "E:\ps\computerswithprinters.txt"
$class = "win32_printer"
$arycomputer = Get-Content "E:\ps\computers.txt"

foreach( $computer in $aryComputer)
{
   Write-Host "Retrieving printers from $computer ..."
   $wmi = Get-WmiObject -Class $class -computername $computer
   format-table -Property name, systemName, shareName -groupby driverName `
                -inputobject $wmi -autosize | Out-File -FilePath $filePath 
}

Thank you in advance!


Solution

  • Try using Out-File -FilePath $filePath -Append