A better way to check if a path exists or not in PowerShell
If you just want an alternative to the cmdlet syntax, specifically for files, use the File.Exists()
.NET method:
if(![System.IO.File]::Exists($path)){
# file with path $path doesn't exist
}
If, on the other hand, you want a general purpose negated alias for Test-Path
, here is how you should do it:
# 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
will now behave exactly like Test-Path
, but always return the opposite result:
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
As you've already shown yourself, the opposite is quite easy, just alias exists
to Test-Path
:
PS C:\> New-Alias exists Test-Path
PS C:\> exists -Path "C:\Windows"
True
The alias solution you posted is clever, but I would argue against its use in scripts, for the same reason I don't like using any aliases in scripts; it tends to harm readability.
If this is something you want to add to your profile so you can type out quick commands or use it as a shell, then I could see that making sense.
You might consider piping instead:
if ($path | Test-Path) { ... }
if (-not ($path | Test-Path)) { ... }
if (!($path | Test-Path)) { ... }
Alternatively, for the negative approach, if appropriate for your code, you can make it a positive check then use else
for the negative:
if (Test-Path $path) {
throw "File already exists."
} else {
# The thing you really wanted to do.
}