특히 파일에 대한 cmdlet 구문의 대안이 필요한 경우 File.Exists().NET 메서드를 사용합니다 .
if(![System.IO.File]::Exists($path)){
# file with path $path doesn't exist
}
반면에에 대한 범용 부정 별칭을 원하는 경우 다음 Test-Path방법을 사용하십시오.
# Gather command meta data from the original Cmdlet (in this case, Test-Path)
$TestPathCmd = Get-Command Test-Path
$TestPathCmdMetaData = New-Object System.Management.Automation.CommandMetadata $TestPathCmd
# Use the static ProxyCommand.GetParamBlock method to copy
# Test-Path's param block and CmdletBinding attribute
$Binding = [System.Management.Automation.ProxyCommand]::GetCmdletBindingAttribute($TestPathCmdMetaData)
$Params = [System.Management.Automation.ProxyCommand]::GetParamBlock($TestPathCmdMetaData)
# Create wrapper for the command that proxies the parameters to Test-Path
# using @PSBoundParameters, and negates any output with -not
$WrappedCommand = {
try { -not (Test-Path @PSBoundParameters) } catch { throw $_ }
}
# define your new function using the details above
$Function:notexists = '{0}param({1}) {2}' -f $Binding,$Params,$WrappedCommand
notexists지금은 행동 할 정확히 같은 Test-Path,하지만 항상 반대의 결과를 반환 :
PS C:\> Test-Path -Path "C:\Windows"
True
PS C:\> notexists -Path "C:\Windows"
False
PS C:\> notexists "C:\Windows" # positional parameter binding exactly like Test-Path
False
이미 자신을 보여준 것처럼, 반대, 아주 쉽게 바로 별칭 exists에 Test-Path:
PS C:\> New-Alias exists Test-Path
PS C:\> exists -Path "C:\Windows"
True
try{ Test-Path -EA Stop $path; #stuff to do if found } catch { # stuff to do if not found }