PowerShell 中 Test-Path 和 Resolve-Path 的区别?
powershellmicrosoft technologiessoftware & coding更新于 2026/2/2 2:07:17
Test-Path 命令检查特定路径是否存在,并返回布尔值输出(True 或 False),而 Resolve-Path 命令则显示特定目录(如果存在),否则抛出异常。例如:
要判断路径是否存在,
示例
PS C:\> Test-Path C:\Temp\ True PS C:\> Resolve-Path C:\Temp\ Path ---- C:\Temp\
由于路径不存在,
PS C:\> Test-Path C:\Temp11\ False PS C:\> Resolve-Path C:\Temp11\ Resolve-Path : Cannot find path 'C:\Temp11\' because it does not exist. At line:1 char:1 + Resolve-Path C:\Temp11\ + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Temp11\:String) [Resolve-Path], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand
Resolve-Path 也可用于通过通配符获取文件内容。例如:
示例
Resolve-Path C:\Temp\*
上述命令将获取 C:\temp 路径下的所有文件和文件夹。
Resolve-Path C:\Temp\web*
上述命令将获取 C:\temp 路径下所有以 Web 开头的文件。
输出
Path ---- C:\Temp\WebImages C:\Temp\web.html C:\Temp\web1.html

