I was not expecting, or wanting, escape sequences in the output. The first line of output is:
0000000000000000 1B 5B 33 32 3B 31 6D 4C 6F 63 61 6C 41 64 64 72 �[32;1mLocalAddr
PS C:\src\t> Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' } | Out-File -FilePath '.\task3.txt' -Encoding ascii
PS C:\src\t> Get-Content .\task3.txt -first 2 | Format-Hex
Label: String (System.String) <5F8755A9>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 1B 5B 33 32 3B 31 6D 4C 6F 63 61 6C 41 64 64 72 �[32;1mLocalAddr
0000000000000010 65 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20 ess
0000000000000020 20 20 20 20 20 20 20 20 20 20 20 4C 6F 63 61 6C Local
0000000000000030 50 6F 72 74 20 52 65 6D 6F 74 65 41 64 64 72 65 Port RemoteAddre
0000000000000040 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ss
0000000000000050 20 20 20 20 20 20 20 20 20 52 65 6D 6F 74 65 50 RemoteP
0000000000000060 6F 72 74 20 53 74 61 74 65 20 20 20 20 20 20 20 ort State
0000000000000070 41 70 70 6C 69 65 64 53 65 74 74 69 6E 67 20 4F AppliedSetting O
0000000000000080 77 6E 69 6E 67 50 72 6F 63 65 73 73 1B 5B 30 6D wningProcess**�[0m**
I note that it does not appear in the output of Get-ChildItem.
PS C:\src\t> dir *.g* | Out-File -FilePath '.\task5.txt' -Encoding ascii
PS C:\src\t> Get-Content -Path '.\task5.txt' -First 2 | Format-Hex
Label: String (System.String) <5A0214B6>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 20 20 20 20 44 69 72 65 63 74 6F 72 79 3A 20 43 Directory: C
0000000000000010 3A 5C 73 72 63 5C 74 :\src\t
CodePudding user response:
"The Out-File cmdlet sends output to a file. It implicitly uses PowerShell's formatting system to write to the file. The file receives the same display representation as the terminal. This means that the output may not be ideal for programmatic processing unless all input objects are strings." - from the documentation.
When I run your lines of code on my system, which has IPV6, those addresses are truncated which is likely another drawback which you may not have spotted.
You don't say what you ultimately want to use the file for, but export-csv
produces more useable results:
Get-NetTCPConnection | Where-Object { $_.State -eq 'Established' } | export-csv '.\task3.txt'
CodePudding user response:
Tom's helpful answer makes an important point: the output from Out-*
cmdlets is intended for human eyeballs, not for programmatic processing.
Leaving that aside:
The fact that ANSI / Virtual Terminal escape sequences - for producing colored output in this case - are present in your output file implies that you're using PowerShell (Core) 7.2 , where the value of $PSStyle.OutputRendering
determines whether these escape sequences are retained in output from - implicit or explicit - calls to Out-*
cmdlets:
Use
$PSStyle.OutputRendering = 'Host'
to restrict use of escape sequences in formatted output to output sent to the display (host / console / terminal) , and to make all other uses - sending output to a file with>
(effectively an alias ofOut-File
) or through the pipeline - result in removal of these escape sequences to produce plain-text output.- Add
$PSStyle.OutputRendering = 'Host'
to your$PROFILE
file to make this behavior the default for future sessions.
- Add
Regrettably, as of PowerShell 7.2.1, the default value of
$PSStyle.OutputRendering
is'Ansi'
, which unconditionally includes the escape sequences in formatted output.'Host'
would make for a much more sensible default, in line with the behavior exhibited by CLIs on Unix-like platforms, which themselves bypass use of escape sequences when their stdout output isn't connected to a terminal - except if explicitly requested to include them, with options such as--color=always