How can I test in PowerShell code if a folder is a junction point?
相关问题
- Why am I getting UnauthorizedAccessException on th
- How to Debug/Register a Permanent WMI Event Which
- How can I do variable substitution in a here-strin
- How to use a default value with parameter sets in
- Does powershell have a method_missing()?
相关文章
- 在vscode如何用code runner打开独立的控制台窗口,以及设置好调试模式时窗口的编码?
- C#调用PowerShell的问题
- EscapeDataString having differing behaviour betwee
- PowerShell Change owner of files and folders
- Command line escaping single quote for PowerShell
- Is there a simple way to pass specific *named* Pow
- How do I access an element with xpath with a names
- How to 'Grant Permissions' Using Azure Act
If (Get-Item Test Folder).Attributes.ToString().Contains("ReparsePoint"){Code}
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:
To check if a file/folder is a "ReparsePoint", of any kind (Junction, SymbolicLink or HardLink):
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:
Note that value 'SymbolicLink' is the same for both file or folder, so to list only Symbolic links to folders:
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 showsl
for both Junction and SymbolicLink: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
FYI, if you happen to be running PowerShell Community Extensions, this info is available as output (and as a note property) on output of Get-ChildItem:
However for programmatic access I would access the info via the Attributes property as the other poster suggests.
Take a look at this blog: http://blogs.msdn.com/powershell/archive/2010/02/10/viewing-junctions-with-dir.aspx
the way to do it is to copy the built in file system formatting file, modify it so that junctions are indicated, then load it with Update-FormatData:
From the Blog:
If you have the PowerShell Community Extensions which I would recommend if you are working with junctions you can do the following to determine if a folder is a junction or not:
Try this: