PowerShell:测试路径 -IsValid 不工作

PowerShell: Test-Path -IsValid not working

PowerShell cmdlet Test-Path :c\df -IsValid(和变体 Test-Path -IsValid :c\df)返回 true 的语法显然无效。有人可以阐明这一点吗?我错过了什么吗?

PS D:\GitHub\PowerShell_Scripts> Test-Path :c\df -IsValid

True

PS D:\GitHub\PowerShell_Scripts> Test-Path -IsValid :c\df

True

PS D:\GitHub\PowerShell_Scripts> mkdir :c\df

New-Item: The filename, directory name, or volume label syntax is incorrect. : 
'D:\GitHub\PowerShell_Scripts\:c\df'
New-Item: The filename, directory name, or volume label syntax is incorrect. : 
'D:\GitHub\PowerShell_Scripts\:c\df'

PS D:\GitHub\PowerShell_Scripts>

为了我提供的例子的目的,下面是一个解决方案:

try { mkdir :c\df } catch {write-output "Invalid Path"; exit}

随着这变成一个更大的脚本,我在 IF-THEN 条件语句中加入了这样的语句:

    # Check whether ":c\df" (which is a variable in my script) exists.
    # If it doesn't exist, TRY-CATCH by creating/deleting folder

    $path=":c\df"
    if (!(Test-Path -Path "$path"))
    {
        try
        {
            # Depending on command, user may want -ErrorAction as a switch

            New-Item -ItemType "directory" -Path "$path" [-ErrorAction Stop]

            # If ":c\df" had been correct, "c:\df", it would have created the folder.
            # Remove new folder (if only interested in testing mkdir of $path)

            Remove-Item -Path "$path"
        }
        catch
        {
            # For my purposes, I will run additional commands/logic to create
            # a script generated unique folder to work with so my script can
            # continue to function. The user can change it when finished.
            # Alternatively, the script could inform the user of an error and exit

            <commands to create new unique folder, $newFolder>

            $path=".$newFolder"
        }
    }