Для примера, который я представил, решение является следующим:
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"
}
}