Skip to content

Need the ability to opt into / out of following symlinks with Get-ChildItem -Recurse, behavior differs between editions #3951

@mklement0

Description

@mklement0

Currently, the behavior with respect to following symlinks to directories with Get-ChildItem -Recurse:

  • differs between editions

  • is invariable in either case.

Windows PowerShell always follows directory symlinks, whereas PowerShell Core never does - due to #3780.
Note that the Core behavior also applies on Windows.

Given that Get-ChildItem is not just the analog of ls, but also of find, there should at least be a way to opt in with respect to symlink following, as with find -L.

Note that reintroducing symlink recursion would reintroduce the need to detect symlink loops, as reported in #1875.

Unless infeasible due to backward compatibility concerns, it's also worth considering changing the Windows edition's default behavior to match the Core edition's.

Steps to reproduce

Note: On Windows, you must run the code from an elevated console.

# Create 2 temp. dirs.
$tmpDirRoot = [IO.Path]::GetTempPath()
$tmpDir1 = join-path $tmpDirRoot "$PID-a"
$tmpDir2 = join-path $tmpDirRoot "$PID-b"
$null = New-Item -Type Directory -Path $tmpDir1, $tmpDir2

Push-Location $tmpDir1

# Create 1 file each.
'' > "$tmpDir1/f1"
'' > "$tmpDir2/f2"

# In tmpDir1, create a symlink to tmpDir2
$null = New-Item -Type SymbolicLink dirlink -Value $tmpDir2

# Recurse and count the items.
# If the directory symlink is followed, 3 items are reported, otherwise 2.
$count = (Get-ChildItem -Recurse).Count

[pscustomobject] @{ 'Actual count' = $count; 'Count w/ following' = 3; 'Count w/o following' = 2 }

# Comment out the following 2 lines to prevent cleanup.
Pop-Location
Remove-Item -Force -Recurse $tmpDir1, $tmpDir2

Actual behavior

Windows PowerShell:

Actual count Count w/ following Count w/o following
------------ ------------------ -------------------
           3                  3                   2

PowerShell Core:

Actual count Count w/ following Count w/o following
------------ ------------------ -------------------
           2                  3                   2

Environment data

PowerShell Core v6.0.0-beta (v6.0.0-beta.2) on macOS 10.12.5
PowerShell Core v6.0.0-beta (v6.0.0-beta.2) on Ubuntu 16.04.1 LTS
PowerShell Core v6.0.0-beta (v6.0.0-beta.2) on Microsoft Windows 10 Pro (64-bit; v10.0.14393)
Windows PowerShell v5.1.14393.1198 on Microsoft Windows 10 Pro (64-bit; v10.0.14393)

Metadata

Metadata

Assignees

Labels

Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions