Skip to content

Copy-Item does not understand symlinks #2957

Closed

Description

Steps to reproduce

~/test |-/ mkdir folder
~/test |-/ touch file
~/test |-/ ln -s folder validfolderlink
~/test |-/ ln -s file validfilelink
~/test |-/ ln -s foobarbaz brokenlink
~/test |-/ Copy-Item ./validfolderlink validfolderlinkcopy
~/test |-/ ls -l validfolderlinkcopy
total 0
~/test |-/ Copy-Item ./validfilelink validfilelinkcopy
~/test |-/ ls -l ./validfilelinkcopy
-rw-rw-r--. 1 andschwa andschwa 0 Jan  5 13:29 ./validfilelinkcopy
~/test |-/ Copy-Item ./brokenlink brokenlinkcopy
Copy-Item : Could not find file '/home/andschwa/test/brokenlink'.
At line:1 char:1
+ Copy-Item ./brokenlink brokenlinkcopy
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.CopyItemCommand

~/test |-/

Expected behavior

  1. validfolderlinkcopy should be a copy of the symlink validfolderlink
  2. validfilelinkcopy should be a copy of the symlink validfilelink
  3. brokenlinkcopy should be a copy of the symlink brokenlink

Actual behavior

  1. validfolderlinkcopy is a copy of link's target folder
  2. validfilelinkcopy is a copy of the link's target file
  3. PowerShell throws an error when copying brokenlink to brokenlinkcopy

Environment data

> $PSVersionTable
v6.0.0-alpha.14
> uname -nro
andschwa-centos 3.10.0-514.2.2.el7.x86_64 GNU/Linux

This is similar to #804 where New-Item did not understand broken symlinks, since it was trying to follow it. The semantics on Linux of copy/move/new is to treat the symlink as a normal file, instead of following it, but PowerShell does not do this.

Activity

  1. added
    WG-Cmdlets
    Issue-Bug
    on Jan 5, 2017
  2. andyleejordan commented on Apr 30, 2018

    @andyleejordan
    MemberAuthor

    I assume this is covered by other bugs or fixed; and if not, at this point it hasn't been a big bug I guess.

  3. mcleary commented on Apr 8, 2020

    @mcleary

    I am a bit confused by the resolution of this issue. I am trying to do the exact same thing, copy a symlink but I get the same behaviour and I can't find an option to preserve the links when copying an item.

    Is this issue really solved?

  4. andyleejordan commented on Apr 9, 2020

    @andyleejordan
    MemberAuthor

    Joey Aiello (@joeyaiello) Can you triage this please?

  5. andyleejordan commented on Mar 30, 2021

    @andyleejordan
    MemberAuthor

    This never got triaged and I don't know if it's still an issue, just closing for now.

  6. kevinholtkamp commented on Apr 3, 2022

    @kevinholtkamp

    Andy Jordan (@andschwa) this is still an issue (at least for me on Windows)
    When I use Copy-Item to copy a Symlink, it creates a new folder instead of creating a new Symlink with the same Target

  7. shilan commented on Nov 7, 2024

    @shilan

    I have a script like this

          - name: Copy License File
            shell: powershell
            run: |
              $source = "${{ runner.temp }}\E.lic"
              $destinationDir = "${{ vars[format('E_DEPLOY_PATH_{0}', matrix.site)] }}"
              $destinationFile = Join-Path -Path $destinationDir -ChildPath "E.lic"
              Write-Host "Copying $source to $destinationFile"
              Copy-Item -Path $source -Destination $destinationFile -Force
    

    instead of copying it the file, it just creates a folder called E.lic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions