从 Windows 10 开始,PowerShell 终于能够在本地创建连接和链接。
但是,Remove-Item 函数似乎不知道该联结并尝试删除要求确认的目录以及它是否应该递归删除其中的项目。
所以,问题是:有没有办法使用 PowerShell 原生 Cmdlet 删除连接?(即不调用cmd)
从 Windows 10 开始,PowerShell 终于能够在本地创建连接和链接。
但是,Remove-Item 函数似乎不知道该联结并尝试删除要求确认的目录以及它是否应该递归删除其中的项目。
所以,问题是:有没有办法使用 PowerShell 原生 Cmdlet 删除连接?(即不调用cmd)
有没有办法使用 PowerShell 删除联结?
目前,至少在 PowerShell v5 中,这被认为是“固定的”。你可以做的是使用-Force
开关,否则你会得到一个错误,将路径称为 NTFS 结点。我至少使用引号固定的原因是使用开关仍然会使有关目录中子项的消息显示出来。选择 Y 仍然只会删除我使用 PSv5 进行的测试中的交汇点。
Remove-Item "C:\temp\junction" -Force -Confirm:$False
如果这对您不起作用或者您没有 v5,您可以使用 .Net 方法删除目录。这似乎也可以正常工作。
[io.directory]::Delete("C:\temp\junction")
简单的命令 -
rm [path of file] -Force
谷歌搜索了半天,终于找到了答案:
function Remove-Any-File-Force ($Target) {
if ( Test-Path -Path "$Target" ){
& $env:SystemRoot\System32\ATTRIB.exe -S -H -R "$Target" >$null 2>$null
} else {
return
}
$TargetAttributes = (Get-Item -Path $Target -Force).Attributes.ToString()
if ($TargetAttributes -match "ReparsePoint") {
if ($TargetAttributes -match "Archive") {
Remove-Item -Path "$Target" -Force
} else {
try {
& $env:SystemRoot\System32\cmd.exe /c rmdir /Q "$Target" >$null 2>$null
} catch {
try {
[io.directory]::Delete("$Target")
} catch {
Remove-Item -Path "$Target" -Force
}
}
}
} else {
if ($TargetAttributes -match "Directory") {
Remove-Item -Path "$Target" -Force -Recurse
} else {
Remove-Item -Path "$Target" -Force
}
}
}