You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PowerShell中FileInfo和DirectoryInfo的默认显示视图?

如何让PowerShell的Get-ChildItem默认只显示完整文件/目录路径?

问题原因

你之前的自定义格式文件仅对System.IO.DirectoryInfo配置了新视图,System.IO.FileInfo的默认视图未被正确覆盖,导致文件仍显示默认属性信息。PowerShell格式系统会优先匹配最贴合的视图定义,之前的配置未针对FileInfo的默认children视图做替换。

正确的自定义格式文件(format.ps1xml)

以下配置会同时覆盖文件和目录的默认显示逻辑,直接输出完整路径:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <ViewDefinitions>
    <!-- 覆盖DirectoryInfo的默认children视图 -->
    <View>
      <Name>children</Name>
      <ViewSelectedBy>
        <TypeName>System.IO.DirectoryInfo</TypeName>
      </ViewSelectedBy>
      <WideControl>
        <WideEntries>
          <WideEntry>
            <WideItem>
              <PropertyName>FullName</PropertyName>
            </WideItem>
          </WideEntry>
        </WideEntries>
      </WideControl>
    </View>

    <!-- 覆盖FileInfo的默认children视图 -->
    <View>
      <Name>children</Name>
      <ViewSelectedBy>
        <TypeName>System.IO.FileInfo</TypeName>
      </ViewSelectedBy>
      <WideControl>
        <WideEntries>
          <WideEntry>
            <WideItem>
              <PropertyName>FullName</PropertyName>
            </WideItem>
          </WideEntry>
        </WideEntries>
      </WideControl>
    </View>

    <!-- 兼容带硬链接的目录场景 -->
    <View>
      <Name>childrenWithHardlink</Name>
      <ViewSelectedBy>
        <TypeName>System.IO.DirectoryInfo</TypeName>
      </ViewSelectedBy>
      <WideControl>
        <WideEntries>
          <WideEntry>
            <WideItem>
              <PropertyName>FullName</PropertyName>
            </WideItem>
          </WideEntry>
        </WideEntries>
      </WideControl>
    </View>
  </ViewDefinitions>
</Configuration>

配置说明

  • 使用WideControl实现单行输出,符合直接展示路径的需求;
  • 直接调用FullName属性比脚本块更高效,避免额外计算;
  • 分别为FileInfo和DirectoryInfo定义同名的children视图,确保覆盖系统默认配置;
  • 补充childrenWithHardlink视图,避免特殊目录场景下的显示异常。

加载配置的步骤

  1. 将上述内容保存为CustomPathView.ps1xml;
  2. 执行命令加载配置:
Update-FormatData -PrependPath "C:\Users\user\Desktop\CustomPathView.ps1xml"
  • -PrependPath参数确保自定义视图优先级高于系统默认视图。

替代方案:自定义函数

如果不想修改格式文件,可以直接定义一个替代gci的函数,添加到PowerShell配置文件($PROFILE)中永久生效:

function gci {
    Get-ChildItem @args | Select-Object -ExpandProperty FullName
}

内容的提问来源于stack exchange,提问作者Podbrushkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:55:54