How can I test in PowerShell code if a folder is a junction point?
Since at least PowerShell v5.0, there is better support for links (or as MS calls them: Reparse Points)
improved Item cmdlets - LinkType property
Linked article is under WMF 5.0 category, which might mean that the method was available since PS v5.0.
These features were included in standard Get-Item, Get-ChildItem, so there are no additional steps required. It can be used on any current PS.
LinkType is a String property on an Object, returned by Get-Item and Get-ChildItem,
it can have one of the following four values: '', 'Junction', 'SymbolicLink', 'HardLink'.
To answer OP's question, you can check if a folder is a junction point using:
if ((Get-Item -Path $Target -Force).LinkType -eq "Junction") { }
To check if a file/folder is a "ReparsePoint", of any kind (Junction, SymbolicLink or HardLink):
if ((Get-Item -Path $Target -Force).LinkType) { }
LinkType value on ordinary file/folder is an empty String, which when used as if condition in PS resolves to False
Get-ChildItem can be used to list all Junction folders:
(Get-ChildItem -Path $Target -Force) | Where-Object { $_.LinkType -eq "Junction" }
Note that value 'SymbolicLink' is the same for both file or folder, so to list only Symbolic links to folders:
(Get-ChildItem -Path $Target -Directory -Force) | Where-Object { $_.LinkType -eq "SymbolicLink" }
Cmdlet Get-ChildItem (alias: dir, ls, gci) now shows ReparsePoint attribute, as l
in Mode column, without any extension. But it will not show 'HardLink' and shows l
for both Junction and SymbolicLink:
> Get-ChildItem -Path $Target -Force
Directory: C:\Users
Mode LastWriteTime Length Name
---- ------------- ------ ----
d--hsl 2018-04-12 01:45 All Users
d-rh-- 2018-05-09 06:12 Default
d--hsl 2018-04-12 01:45 Default User
d----- 2018-06-24 03:05 Papo
d-r--- 2018-07-27 07:12 Public
l
on them.Using these improved, or today's standard Cmdlets, has some advantages over previous methods, described in older answers here.
It does differentiate between Junction and Symbolic Link
If OP wanted to test if folder is a Junction, checking by Attribute property would result in false positive for folder Symbolic Link.
detects Hard Link.
LinkType is [String] as opposed to Attributes property, which is [FileAttributes] type and needs .ToString() or a use of -band