当您在命令提示符 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/