PowerShell 1.0可以创建类似Unix的硬链接和软链接吗?

如果这不是内置的,有人能告诉我一个网站,有一个ps1脚本模仿这个吗?

恕我直言,这是任何优秀shell的必要功能。:)


当前回答

我写了一个PowerShell模块,它有MKLINK的本机包装器。https://gist.github.com/2891103

包括以下函数:

New-Symlink New-HardLink New-Junction

捕获MKLINK输出并在必要时抛出适当的PowerShell错误。

其他回答

我写了一个PowerShell模块,它有MKLINK的本机包装器。https://gist.github.com/2891103

包括以下函数:

New-Symlink New-HardLink New-Junction

捕获MKLINK输出并在必要时抛出适当的PowerShell错误。

在Windows 7下,命令为

fsutil hardlink create new-file existing-file

PowerShell发现它没有完整的路径(c:\Windows\system32)或扩展名(.exe)。

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"
}

用法:

New-Symlink "c:\foo\bar" "c:\foo\baz"
Remove-Symlink "c:\foo\bar"

添加“pscx”模块

不,它不是内置在PowerShell中。在Windows Vista/Windows 7上不能单独调用mklink实用程序,因为它是作为“内部命令”直接构建在cmd.exe中。

您可以使用PowerShell社区扩展(免费)。对于不同类型的重解析点,有几个cmdlet:

New-HardLink, New-SymLink, New-Junction, Remove-ReparsePoint 和其他人。

你可以在PowerShell中调用cmd提供的mklink命令来创建符号链接:

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

如果目标是目录,则必须将/d传递给mklink。

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

对于硬链接,我建议使用Sysinternals Junction之类的东西。