用 Resolve-Path 把相对路径转为绝对路径

将 ~、.. 和通配符展开成完整路径,还能用 -Relative 反向输出相对路径。

Windows文件脚本PowerShell低风险
运行 Resolve-Path <路径> 即可把相对或通配符路径解析为绝对路径;加 -Relative 则输出相对路径。

01命令

命令
$ Resolve-Path '..\logs\*.log'

02作用说明

Resolve-Path 基于当前位置解析,'..\logs' 会被展开成如 'C:\Work\logs',与脚本如何被调用无关。每个匹配项返回 PathInfo 对象,.Path 属性就是纯字符串。常被忽略的 -Relative 参数按当前目录输出相对路径;-LiteralPath 可处理含方括号等通配符字符的字面路径。

03风险

风险低风险
使用条件PowerShell
!注意: 路径不存在时 Resolve-Path 会报错;要展开尚未创建的路径,可用 [System.IO.Path]::GetFullPath() 或先解析其父目录再拼接文件名。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Resolve-Path | Microsoft Learn
    Microsoft · vendor-doc · 2026-09-17
    依据: Microsoft documents that Resolve-Path interprets wildcards and relative paths and returns the fully qualified provider path for each match.
  • Resolve-Path - PowerShell
    SS64 · technical · 2026-09-17
    依据: SS64 shows Resolve-Path resolving relative paths, ~, and wildcards to absolute paths, and -Relative for the reverse operation.

最后验证: 2026-09-17 · 分类: 文件与文件夹

06相关方案