AI 制作windwos下powershell目录树工具

79 阅读2分钟

📂 PowerShell 自定义目录树工具 —— stree

stree 是一个 PowerShell 函数,用于在 Windows 11 下打印类似 Linux tree 命令的目录结构。
它支持:

  • 指定显示层级
  • 区分目录/文件(目录后面加 /
  • 忽略指定目录/文件(支持通配符)
  • 彩色高亮
  • 无色模式(方便复制到笔记)

🔧 基本语法

stree [-Path <目录路径>] [-MaxDepth <层数>] [-DirOnly] [-Ignore <名称数组>] [-NoColor]

📌 参数说明

参数类型默认值说明
-Pathstring. (当前目录)要展示的根目录
-MaxDepthint3显示的目录层级
-DirOnlyswitch只显示目录,不显示文件
-Ignorestring[]忽略的目录或文件,支持通配符 (*.pyc, .git*)
-NoColorswitch不使用颜色高亮,便于复制到笔记或 Markdown

📥 安装方法

方法一:临时加载

  1. 打开 PowerShell。

  2. 将 stree 脚本粘贴到当前会话,或保存为 stree.ps1 后执行:

. "路径\到\stree.ps1"

注意开头的点和空格(. "...")表示 点源加载,函数会在当前会话生效。

  1. 现在可以直接使用:

stree -MaxDepth 3

方法二:永久加载(每次打开 PowerShell 都可用)

  1. 打开你的 PowerShell 配置文件(Microsoft.PowerShell_profile.ps1):
$PROFILE    # 可以查看Microsoft.PowerShell_profile.ps1d的目录上

在文件末尾添加:

function stree {
    param(
        [string]$Path = ".",
        [int]$MaxDepth = 3,
        [switch]$DirOnly,
        [string[]]$Ignore = @(),
        [switch]$NoColor
    )

    function Print-Tree($CurrentPath, $Prefix = "", $Depth = 0, $Ignore) {
        if ($Depth -ge $MaxDepth) { return }

        $dirs = Get-ChildItem -Path $CurrentPath -Directory | Sort-Object Name |
            Where-Object { $name = $_.Name; -not ($Ignore | Where-Object { $name -like $_ }) }

        $files = if (-not $DirOnly) {
            Get-ChildItem -Path $CurrentPath -File | Sort-Object Name |
                Where-Object { $name = $_.Name; -not ($Ignore | Where-Object { $name -like $_ }) }
        } else { @() }

        $items = @()
        $items += $dirs
        $items += $files

        for ($i = 0; $i -lt $items.Count; $i++) {
            $item = $items[$i]
            $isLast = ($i -eq $items.Count - 1)
            $branch = if ($isLast) { "└── " } else { "├── " }

            if ($NoColor) {
                $name = if ($item.PSIsContainer) { "$($item.Name)/" } else { $item.Name }
            }
            else {
                if ($item.PSIsContainer) {
                    $colorStart = "`e[94m"   # 亮蓝色
                    $name = "$($item.Name)/"
                } else {
                    $colorStart = "`e[32m"   # 绿色
                    $name = $item.Name
                }
                $colorEnd = "`e[0m"
                $name = "$colorStart$name$colorEnd"
            }

            Write-Output ("$Prefix$branch$name")

            if ($item.PSIsContainer) {
                $newPrefix = if ($isLast) { "$Prefix    " } else { "$Prefix│   " }
                Print-Tree $item.FullName $newPrefix ($Depth + 1) $Ignore
            }
        }
    }

    $rootPath = Resolve-Path $Path
    $rootName = Split-Path -Path $rootPath -Leaf

    if ($NoColor) {
        Write-Output ("$rootName/")
    }
    else {
        Write-Output ("`e[94m$rootName/$`e[0m")   # 根目录也用亮蓝色
    }

    Print-Tree $rootPath "" 0 $Ignore
}

  1. 保存并关闭文件。

  2. 重新打开 PowerShell,即可在任何目录下使用 stree。

$PROFILE 是 PowerShell 的启动脚本路径,每个用户都不一样,执行 $PROFILE 可以查看。

🎯 使用示例

  1. 显示当前目录,最多 2 层
stree 2
  1. 只显示目录
stree -DirOnly
  1. 忽略单个目录
stree -Ignore ".idea"
  1. 忽略多个目录或文件
stree -Ignore ".git","__pycache__","db.sqlite3"
  1. 使用通配符忽略
stree -Ignore "*.pyc","*.log",".git*"
  1. 不使用颜色高亮
stree -NoColor

🎨 默认配色

  1. 目录:亮蓝色 (94)

  2. 文件:绿色 (32)

  3. 树形符号 (├──, └──):默认终端颜色