Creating hard and soft links using PowerShell

前端 未结 11 1698
攒了一身酷
攒了一身酷 2020-12-04 05:04

Can PowerShell 1.0 create hard and soft links analogous to the Unix variety?

If this isn\'t built in, can someone point me to a site that has a ps1 script that mimi

相关标签:
11条回答
  • 2020-12-04 05:22

    New-Symlink:

    Function New-SymLink ($link, $target)
    {
        if (test-path -pathtype container $target)
        {
            $command = "cmd /c mklink /d"
        }
        else
        {
            $command = "cmd /c mklink"
        }
    
        invoke-expression "$command $link $target"
    }
    

    Remove-Symlink:

    Function Remove-SymLink ($link)
    {
        if (test-path -pathtype container $link)
        {
            $command = "cmd /c rmdir"
        }
        else
        {
            $command = "cmd /c del"
        }
    
        invoke-expression "$command $link"
    }
    

    Usage:

    New-Symlink "c:\foo\bar" "c:\foo\baz"
    Remove-Symlink "c:\foo\bar"
    
    0 讨论(0)
  • 2020-12-04 05:24

    I wrote a PowerShell module that has native wrappers for MKLINK. https://gist.github.com/2891103

    Includes functions for:

    • New-Symlink
    • New-HardLink
    • New-Junction

    Captures the MKLINK output and throws proper PowerShell errors when necessary.

    0 讨论(0)
  • 2020-12-04 05:27

    Add "pscx" module

    No, it isn't built into PowerShell. And the mklink utility cannot be called on its own on Windows Vista/Windows 7 because it is built directly into cmd.exe as an "internal command".

    You can use the PowerShell Community Extensions (free). There are several cmdlets for reparse points of various types:

    • New-HardLink,
    • New-SymLink,
    • New-Junction,
    • Remove-ReparsePoint
    • and others.
    0 讨论(0)
  • 2020-12-04 05:28

    You can call the mklink provided by cmd, from PowerShell to make symbolic links:

    cmd /c mklink c:\path\to\symlink c:\target\file
    

    You must pass /d to mklink if the target is a directory.

    cmd /c mklink /d c:\path\to\symlink c:\target\directory
    

    For hard links, I suggest something like Sysinternals Junction.

    0 讨论(0)
  • 2020-12-04 05:30

    Actually, the Sysinternals junction command only works with directories (don't ask me why), so it can't hardlink files. I would go with cmd /c mklink for soft links (I can't figure why it's not supported directly by PowerShell), or fsutil for hardlinks.

    If you need it to work on Windows XP, I do not know of anything other than Sysinternals junction, so you might be limited to directories.

    0 讨论(0)
  • 2020-12-04 05:36

    Try junction.exe

    The Junction command line utility from SysInternals makes creating and deleting junctions easy.

    Further reading

    • MS Terminology: soft != symbolic
      Microsoft uses "soft link" as another name for "junction".
      However: a "symbolic link" is something else entirely.
      See MSDN: Hard Links and Junctions in Windows.
      (This is in direct contradiction to the general usage of those terms where "soft link" and "symbolic link" ("symlink") DO mean the same thing.)
    0 讨论(0)
提交回复
热议问题