5

The following input:

PS STG:\> Get-ChildItem | tree

Gives me

Folder PATH listing for volume Data
Volume serial number is 6576-C540
D:.
ÃÄÄÄAPK
ÃÄÄÄAssets
³   ÃÄÄÄAnimations
³   ³   ÃÄÄÄCharacters
³   ³   ³   ÀÄÄÄinhabitant
³   ³   ÃÄÄÄEnvironment
³   ³   ÀÄÄÄProps
³   ÃÄÄÄMaterials
³   ³   ÃÄÄÄCharacters
³   ³   ³   ÀÄÄÄinhabitant
³   ³   ÃÄÄÄEnvironment
³   ³   ÃÄÄÄParticles
³   ³   ³   ÀÄÄÄasteroidParticle
³   ³   ÀÄÄÄProps
³   ÃÄÄÄMeshes
³   ³   ÃÄÄÄCharacters
³   ³   ³   ÀÄÄÄinhabitant
³   ³   ÃÄÄÄEnvironment
³   ³   ÀÄÄÄProps

...etc.

When I was expecting something formatted more like:

├───APK
├───Assets
    └───Animations
    |   └───Characters
    |   |   └───inhabitant
    |   ├───Environment
    |   └───Props
    └───Materials
    |   └───Characters
    |   |   └───inhabitant
    |   ├───Environment
    |   └───Particles
    |   |   └───asteroidParticle
    |   └───Props
    └───Meshes
        └───Characters
        |   └───inhabitant
        ├───Environment
        └───Props

What am I doing / understanding wrong?

This is the value of $OutputEncoding:

IsSingleByte      : True
BodyName          : iso-8859-1
EncodingName      : Western European (Windows)
HeaderName        : Windows-1252
WebName           : Windows-1252
WindowsCodePage   : 1252
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
EncoderFallback   : System.Text.InternalEncoderBestFitFallback
DecoderFallback   : System.Text.InternalDecoderBestFitFallback
IsReadOnly        : True
CodePage          : 1252`
Kevin Panko
  • 7,346
  • 22
  • 44
  • 53
  • @MHA, please copy the detail into your original post - [the post can be edited](http://superuser.com/posts/745083/edit). – Dave Apr 23 '14 at 14:36
  • It definitely looks like an encoding problem. My tree works and the encoding is us-ascii. IsSingleByte : True BodyName : us-ascii EncodingName : US-ASCII HeaderName : us-ascii WebName : us-ascii WindowsCodePage : 1252 IsBrowserDisplay : False IsBrowserSave : False IsMailNewsDisplay : True IsMailNewsSave : True EncoderFallback : System.Text.EncoderReplacementFallback DecoderFallback : System.Text.DecoderReplacementFallback IsReadOnly : True CodePage : 20127 – Keltari Apr 23 '14 at 14:40
  • Im trying to find a way to change to code page... not having much luck, but Im sure thats the issue – Keltari Apr 23 '14 at 14:47
  • @Keltari - you can change the output encoding with `$OutputEncoding = [System.Text.Encoding]::GetEncoding()`, but I am not able to repro OP's issue by setting the code page to `1252` – Rynant Apr 24 '14 at 14:36
  • 2
    @MHA Does using the ascii switch for tree help? `gci| tree /A` – Rynant Apr 24 '14 at 14:40
  • There is also `chcp` to get and set the console's code page, but I was not able to see any change in the display by changing it. http://technet.microsoft.com/en-us/library/bb490874.aspx – Rynant Apr 24 '14 at 15:05
  • Tree isn't a powershell command that I know of, and doesn't accept piped input. `tree` by itself should give you the tree you want. – Eris Jul 25 '14 at 22:20
  • @Eris Tree is available in PowerShell community extensions. However, the OP hasn't explicitly mentioned whether they're using that or if PowerShell is falling back to CMD's tree command. – Iszi Oct 15 '14 at 14:15
  • @MHA - Please provide the output of `Get-Command tree`. – Iszi Oct 15 '14 at 14:15
  • Since I asked this questions I have moved to a different machine. When I run "tree" it works as intended. Thank you for your input. If you formulate it as an answer, I will accept it. – aPerfectMisterMan Oct 17 '14 at 06:33

5 Answers5

1

The default Command Prompt console encoding is codepage 437 and tree.com uses extended characters by default. PowerShell ISE's default console output codepage is 1252. Here are some ways to get around this:

  1. Use tree.com /A to force tree to use ASCII instead of extended characters.
  2. Change the console output encoding in PowerShell to codepage 437 with chcp 437; [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(437).
  3. Convert to bytes and re-encode the output from tree.com | %{ [System.Text.Encoding]::GetEncoding('IBM437').GetString([Console]::OutputEncoding.GetBytes($_)) }

Note that option 2 may have impacts on how some other things display.

JamieSee
  • 141
  • 3
1

tree does not do what you think it does.

tree is not a PowerShell cmdlet. It wasn’t back when this question was asked and it isn’t now on PowerShell 5.1. It is a DOS command. It does not accept any input. When you do gci | tree, you are not using the output of Get-ChildItem at all.

The only correct solution is not to use tree but a proper PowerShell cmdlet instead.

Daniel B
  • 60,360
  • 9
  • 122
  • 163
0

Depending on the version of PowerShell and the presence or absence of community extensions, you can actually end up calling C:\Windows\System32\tree.com. The gibberish is because PowerShell is not using the correct encoding to display the extended characters that tree.com uses by default. You can use chcp.com to see what the command processor's current code page setting is. The usual default is code page 437. PowerShell uses code page 1252 by default. There are to options for dealing with this that work well and are both low-impact and simple.

  1. Just use tree '/A'.
  2. Convert the output from tree to bytes and load it with the proper code page using the following code:

tree | %{ [System.Text.Encoding]::GetEncoding('IBM437').GetString($OutputEncoding.GetBytes($_)) }

JamieSee
  • 141
  • 3
0

Try using the Show-Tree commandlet.

09:17 $ Show-Tree C:\Chocolatey\ -Depth 1
C:\Chocolatey\
|--bin
\--lib
John Oxley
  • 727
  • 4
  • 12
  • 23
-1

Are you running the command in Powershell or Powershell ISE?

There's a difference.

This is what I get in Powershell ISE:

PS C:\powershell> Get-ChildItem | tree
Folder PATH listing
Volume serial number is XXXX-XXXX
C:.
ÃÄÄÄFolder 1
³   ÃÄÄÄSubfolder 1
³   ÃÄÄÄSubfolder 2
³   ÃÄÄÄSubfolder 3
³   ÀÄÄÄSubfolder 4
ÃÄÄÄFolder 2
³   ÃÄÄÄSubfolder 1
³   ÃÄÄÄSubfolder 2
³   ÃÄÄÄSubfolder 3
³   ÀÄÄÄSubfolder 4
ÃÄÄÄFolder 3
³   ÃÄÄÄSubfolder 1
³   ÃÄÄÄSubfolder 2
³   ÃÄÄÄSubfolder 3
³   ÀÄÄÄSubfolder 4
ÀÄÄÄFolder 4
    ÃÄÄÄSubfolder 1
    ÃÄÄÄSubfolder 2
    ÃÄÄÄSubfolder 3
    ÀÄÄÄSubfolder 4

PS C:\powershell> 

This is what I get in Powershell:

PS C:\powershell> Get-childitem | tree
Folder PATH listing
Volume serial number is XXXX-XXXX
C:.
+---Folder 1
¦   +---Subfolder 1
¦   +---Subfolder 2
¦   +---Subfolder 3
¦   +---Subfolder 4
+---Folder 2
¦   +---Subfolder 1
¦   +---Subfolder 2
¦   +---Subfolder 3
¦   +---Subfolder 4
+---Folder 3
¦   +---Subfolder 1
¦   +---Subfolder 2
¦   +---Subfolder 3
¦   +---Subfolder 4
+---Folder 4
    +---Subfolder 1
    +---Subfolder 2
    +---Subfolder 3
    +---Subfolder 4

PS C:\powershell>
Kevin Panko
  • 7,346
  • 22
  • 44
  • 53
  • Interesting point here, but I must say my experience differs - tree output is the same regardless of using console or ISE. Do you have the community extensions installed, or is this using CMD's tree? – Iszi Oct 15 '14 at 14:17