Test-Path isValid parameter

S

Shay Levi

From the help file

-isValid <SwitchParameter>
Determines whether the syntax of the path is correct, regardless of whether
the elements of the path exist. Returns TRUE if the
path syntax is valid; FALSE if it is not.


Should I rely on Test-Path's -isValid parameter ???


PS C:\Scripts> Test-Path c:\ -isValid
True

PS C:\Scripts> Test-Path c:\]] -isValid
True

PS C:\Scripts> Test-Path c:\\\ -isValid
True

PS C:\Scripts> Test-Path \\\ -isValid
False

PS C:\Scripts> Test-Path z\\\ -isValid
True

PS C:\Scripts> Test-Path z\\\// -isValid
True

PS C:\Scripts> Test-Path shay-levi -isValid
True



Shay
http://scriptolog.blogspot.com
 

My Computer

O

Oisin Grehan

On Sep 19, 6:26 pm, Shay Levi <[email protected]> wrote:

> From the help file
>
> -isValid <SwitchParameter>
> Determines whether the syntax of the path is correct, regardless of whether
> the elements of the path exist. Returns TRUE if the
> path syntax is valid; FALSE if it is not.
>
> Should I rely on Test-Path's -isValid parameter ???
>
> PS C:\Scripts> Test-Path c:\ -isValid
> True
>
> PS C:\Scripts> Test-Path c:\]] -isValid
> True
>
> PS C:\Scripts> Test-Path c:\\\ -isValid
> True
>
> PS C:\Scripts> Test-Path \\\ -isValid
> False
>
> PS C:\Scripts> Test-Path z\\\ -isValid
> True
>
> PS C:\Scripts> Test-Path z\\\// -isValid
> True
>
> PS C:\Scripts> Test-Path shay-levi -isValid
> True
>
> Shayhttp://scriptolog.blogspot.com

should you rely on it for what, exactly? it's about syntactic
validity, not exists etc.

- Oisin
 

My Computer

K

Kiron

The -isValid switch parameter checks for the presence of 'invalid' filename
characters...
" * : < > ? |
....if present it returns $false.

32..255|?{!(test-path "$([char]$_)" -isValid)}|%{[char]$_}

Could be used to ensure the validity of randomly generated file names for
example.

--
Kiron
 

My Computer

K

kirk

On Sep 20, 5:07 pm, "Kiron" <[email protected]> wrote:

> The -isValid switch parameter checks for the presence of 'invalid' filename
> characters...
> " * : < > ? |
> ...if present it returns $false.
>
> 32..255|?{!(test-path "$([char]$_)" -isValid)}|%{[char]$_}
>
> Could be used to ensure the validity of randomly generated file names for
> example.

Hi Kiron,

I agree, although note that if you use
[System.IO.Path]::GetRandomFileName() you get a nice random filename
that contains only safe characters:

http://pageofwords.com/blog/default.aspx?page=admin

Kirk
 

My Computer

S

Shay Levi

Thank you guys.

Shay
http://scriptolog.blogspot.com



> From the help file
>
> -isValid <SwitchParameter>
> Determines whether the syntax of the path is correct, regardless
> of whether
> the elements of the path exist. Returns TRUE if the
> path syntax is valid; FALSE if it is not.
> Should I rely on Test-Path's -isValid parameter ???
>
> PS C:\Scripts> Test-Path c:\ -isValid
> True
> PS C:\Scripts> Test-Path c:\]] -isValid
> True
> PS C:\Scripts> Test-Path c:\\\ -isValid
> True
> PS C:\Scripts> Test-Path \\\ -isValid
> False
> PS C:\Scripts> Test-Path z\\\ -isValid
> True
> PS C:\Scripts> Test-Path z\\\// -isValid
> True
> PS C:\Scripts> Test-Path shay-levi -isValid
> True
> Shay
> http://scriptolog.blogspot.com
 

My Computer

Top