执行完成后删除 self 文件夹

delete the self folder once execution completed

我可以在执行完成后删除脚本。

function Delete() {
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    $Path =  $Invocation.MyCommand.Path 
    Write-Host $Path 
    Remove-Item $Path
            }       
Delete  

但是我无法删除脚本所在的整个文件夹和子文件夹 运行。有什么方法可以解决这个问题,同时适用于 powershell2.0 和 3.0?

您可能必须使用 -recurse-force 参数。您还必须使用 e 获取脚本文件夹。 G。 Split-Path 命令:

function Delete() {
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    $Path =  $Invocation.MyCommand.Path 
    Write-Host $Path 
    Remove-Item (Split-Path $Path) -recurse -force
            }       
Delete