A better way to check if a path exists or not in PowerShell

前端 未结 7 2163
名媛妹妹
名媛妹妹 2020-11-29 20:32

I just don\'t like the syntax of:

if (Test-Path $path) { ... }

and

if (-not (Test-Path $path)) { ... }
if (!(Test-Path $pa         


        
7条回答
  •  Happy的楠姐
    2020-11-29 21:28

    Add the following aliases. I think these should be made available in PowerShell by default:

    function not-exist { -not (Test-Path $args) }
    Set-Alias !exist not-exist -Option "Constant, AllScope"
    Set-Alias exist Test-Path -Option "Constant, AllScope"
    

    With that, the conditional statements will change to:

    if (exist $path) { ... }
    

    and

    if (not-exist $path) { ... }
    if (!exist $path) { ... }
    

提交回复
热议问题