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`
10
  • @MHA, please copy the detail into your original post - the post can be edited.
    – Dave
    Apr 23, 2014 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, 2014 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, 2014 at 14:47
  • @Keltari - you can change the output encoding with $OutputEncoding = [System.Text.Encoding]::GetEncoding(<code page>), but I am not able to repro OP's issue by setting the code page to 1252
    – Rynant
    Apr 24, 2014 at 14:36
  • 2
    @MHA Does using the ascii switch for tree help? gci| tree /A
    – Rynant
    Apr 24, 2014 at 14:40

5 Answers 5

2

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.

1

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($_)) }

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.

0

Try using the Show-Tree commandlet.

09:17 $ Show-Tree C:\Chocolatey\ -Depth 1
C:\Chocolatey\
|--bin
\--lib
-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>
1
  • 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, 2014 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .