Creating hard and soft links using PowerShell

前端 未结 11 1699
攒了一身酷
攒了一身酷 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:40

    I combined two answers (@bviktor and @jocassid). It was tested on Windows 10 and Windows Server 2012.

    function New-SymLink ($link, $target)
    {
        if ($PSVersionTable.PSVersion.Major -ge 5)
        {
            New-Item -Path $link -ItemType SymbolicLink -Value $target
        }
        else
        {
            $command = "cmd /c mklink /d"
            invoke-expression "$command ""$link"" ""$target"""
        }
    }
    
    0 讨论(0)
  • 2020-12-04 05:42

    You can use this utility:

    c:\Windows\system32\fsutil.exe create hardlink
    
    0 讨论(0)
  • 2020-12-04 05:44

    Windows 10 (and Powershell 5.0 in general) allows you to create symbolic links via the New-Item cmdlet.

    Usage:

    New-Item -Path C:\LinkDir -ItemType SymbolicLink -Value F:\RealDir
    

    Or in your profile:

    function make-link ($target, $link) {
        New-Item -Path $link -ItemType SymbolicLink -Value $target
    }
    

    Turn on Developer Mode to not require admin privileges when making links with New-Item:

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

    I found this the simple way without external help. Yes, it uses an archaic DOS command but it works, it's easy, and it's clear.

    $target = cmd /c dir /a:l | ? { $_ -match "mySymLink \[.*\]$" } | % `
    {
        $_.Split([char[]] @( '[', ']' ), [StringSplitOptions]::RemoveEmptyEntries)[1]
    }
    

    This uses the DOS dir command to find all entries with the symbolic link attribute, filters on the specific link name followed by target "[]" brackets, and for each - presumably one - extracts just the target string.

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

    In Windows 7, the command is

    fsutil hardlink create new-file existing-file
    

    PowerShell finds it without the full path (c:\Windows\system32) or extension (.exe).

    0 讨论(0)
提交回复
热议问题