'Export Powershell to txt file

I have a powershell script that I run and would like it to export to a txt file when completed. But it should not overwrite the existing output file Any suggestions?

tracert  9.9.9.9
tracert  1.1.1.1
tracert  8.8.4.4
tracert  149.112.112.112
tracert  8.8.8.8
PAUSE 


Solution 1:[1]

You could add the cmdlet Start-Transcript to the script to have any output it generates recorded in a text file.

Alternatively you could save and run the script and then redirect its output to a file:

.\yourscript.ps1 > output.txt

Or within the script you could capture the output of your commands and then use a cmdlet like Out-File to to send the result to file.

Solution 2:[2]

Normally there's an easy difference between overwriting and appending a file:

>output.txt  // overwrite
>>output.txt // append

Solution 3:[3]

Depending on what version of PowerShell you are using and what OS you are using, there is a built-in PowerShelCmdlet for this.

'9.9.9.9','1.1.1.1','8.8.4.4','149.112.112.112','8.8.8.8' | 
ForEach {Test-NetConnection -ComputerName $PSItem -TraceRoute} |
Out-File -FilePath 'D:\Temp\TraceRoutReport.csv' -Append

You could do the same approach with the what you are doing as well. Yet, if you are in PowerShell, use the cmdlets.

Windows PowerShell equivalents for common networking commands (IPCONFIG, PING, NSLOOKUP, TRACERT)

TRACERT

Description: Trace route. Shows the IP route to a host, including all the hops between your computer and that host. PowerShell: Test-NetConnection –TraceRoute

Sample command lines:

Test-NetConnection www.microsoft.com –TraceRoute
Test-NetConnection outlook.com -TraceRoute | Select -ExpandProperty TraceRoute | % { Resolve-DnsName $_ -type PTR -ErrorAction SilentlyContinue }

Sample output:

PS C:\> Test-NetConnection www.microsoft.com –TraceRoute
ComputerName           : www.microsoft.com
RemoteAddress          : 104.66.197.237
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.1.2
PingSucceeded          : True
PingReplyDetails (RTT) : 16 ms
TraceRoute             : 192.168.1.1
                         10.0.0.1
                         TimedOut
                         68.86.113.181
                         69.139.164.2
                         68.85.240.94
                         68.86.93.165
                         68.86.83.126
                         104.66.197.237
PS C:\> Test-NetConnection outlook.com -TraceRoute | Select -ExpandProperty TraceRoute | % { Resolve-DnsName $_ -type PTR -ErrorAction SilentlyContinue }
Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
125.144.85.68.in-addr.arpa     PTR    7200  Answer     te-0-1-0-10-sur02.bellevue.wa.seattle.comcast.net
142.96.86.68.in-addr.arpa      PTR    4164  Answer     be-1-sur03.bellevue.wa.seattle.comcast.net
6.164.139.69.in-addr.arpa      PTR    2469  Answer     be-40-ar01.seattle.wa.seattle.comcast.net
165.93.86.68.in-addr.arpa      PTR    4505  Answer     be-33650-cr02.seattle.wa.ibone.comcast.net
178.56.167.173.in-addr.arpa    PTR    7200  Answer     as8075-1-c.seattle.wa.ibone.comcast.net
248.82.234.191.in-addr.arpa    PTR    3600  Answer     ae11-0.co2-96c-1a.ntwk.msn.net

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Wragg
Solution 2 Dominique
Solution 3