PowerShell 1.0可以创建类似Unix的硬链接和软链接吗?
如果这不是内置的,有人能告诉我一个网站,有一个ps1脚本模仿这个吗?
恕我直言,这是任何优秀shell的必要功能。:)
PowerShell 1.0可以创建类似Unix的硬链接和软链接吗?
如果这不是内置的,有人能告诉我一个网站,有一个ps1脚本模仿这个吗?
恕我直言,这是任何优秀shell的必要功能。:)
当前回答
Windows 10(以及Powershell 5.0)允许您通过New-Item cmdlet创建符号链接。
用法:
New-Item -Path C:\LinkDir -ItemType SymbolicLink -Value F:\RealDir
或者在你的个人资料中:
function make-link ($target, $link) {
New-Item -Path $link -ItemType SymbolicLink -Value $target
}
打开开发人员模式,在新建项目链接时不需要管理员权限:
其他回答
你可以在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之类的东西。
在Windows 7下,命令为
fsutil hardlink create new-file existing-file
PowerShell发现它没有完整的路径(c:\Windows\system32)或扩展名(.exe)。
实际上,Sysinternals junction命令只适用于目录(不要问我为什么),所以它不能硬链接文件。对于软链接,我会使用cmd /c mklink(我不知道为什么PowerShell不直接支持它),对于硬链接,我会使用fsutil。
如果您需要它在Windows XP上工作,除了Sysinternals连接,我不知道还有什么其他方法,所以您可能只能使用目录。
你可以使用这个实用程序:
c:\Windows\system32\fsutil.exe create hardlink
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"