PowerShell dir 显示所有符号链接(symbolic link)目标,如命令提示符

标签 powershell symlink

当您在命令提示符 cmd.exe 中使用 dir 时,您会得到

C:\path\to\somewhere> dir
2022-10-03  20:17    <DIR>          .
2022-10-03  19:54    <DIR>          ..
2022-10-03  20:16    <SYMLINKD>     link [..\target\]

但是当您在 PowerShell 中尝试相同的操作时,您会得到

PS C:\path\to\somewhere> dir
Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----l        2022-10-03     20:16                link

如何让 PowerShell 也像命令提示符一样显示目录中的所有链接目标?

最佳答案

这取决于 Powershell 版本。请记住,dir 只是 Get-ChildItem 的别名,在 PS 5 之前,GCI 根本不包含 linktype目标

对于 PS 5+,这有效:

Get-ChildItem -Path "C:\temp\" -Force |
   Where-Object { $_.LinkType -ne $null } |
   ft FullName,Attributes,Linktype,Target

FullName                  Attributes LinkType     Target
--------                  ---------- --------     ------
C:\temp\hard Directory, ReparsePoint SymbolicLink {C:\Temp\temp\text.txt}
C:\temp\j    Directory, ReparsePoint Junction     {c:\temp\tmp\actss}
C:\temp\soft Directory, ReparsePoint SymbolicLink {C:\Temp\Log4j}

对于 PS 4,没有 Linktype,我们需要检查 ReparsePoints 是否匹配,并且只能得到以下输出:

Get-ChildItem -Path "C:\temp\" -Force |
   Where-Object { $_.LinkType -ne $null -or $_.Attributes -match "ReparsePoint" } |
   ft FullName,Attributes,Linktype,Target -auto

FullName                  Attributes Linktype Target
--------                  ---------- -------- ------
C:\temp\J    Directory, ReparsePoint
C:\temp\soft Directory, ReparsePoint
C:\temp\hard   Archive, ReparsePoint

因此,在后一种情况下,您最好像前面提到的那样使用 cmd/c dir

关于PowerShell dir 显示所有符号链接(symbolic link)目标,如命令提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73942032/

相关文章:

linux - 查看目录 rec 是否在 Linux 中用作符号链接(symbolic link)

c# - 在 Mono 中检测符号链接(symbolic link)和管道

php - PHP 中的符号链接(symbolic link)不起作用

web-services - 如何在本地运行 Powershell DSC 脚本

powershell - PowerShell-脚本属性内的SupportsShouldProcess

powershell - WMI - 使用非管理员帐户查询服务器

Linux 查找带有符号链接(symbolic link)递归和一些额外功能

linux - 在 Linux 中 : How can I create a symbolic link to a file whose name starts with a dash/minus sign ?

visual-studio - 禁止在解决方案中使用多个版本的 NuGet 包

powershell - 对象传递变量在 block 脚本上不起作用