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,466

5 Answers5

3

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

JamieSee
  • 181
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.

user219095
  • 65,551
0

Try using the Show-Tree commandlet.

09:17 $ Show-Tree C:\Chocolatey\ -Depth 1
C:\Chocolatey\
|--bin
\--lib
-2

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,466