Prerequisites
Steps to reproduce
I have a PowerShell function which accepts a file path as input and performs a number of data retrieval/manipulation tasks, before eventually passing the data and the file path to a cmdlet in a third-party module, which transforms the data, creates the specified file, and populates it with the transformed data.
I need to be able to test whether the provided file path is valid, (even if it doesn't exist yet), before passing it to the third-party cmdlet, but none of the various validation methods I can find online seem to work as expected in current versions of PowerShell (7.3.x or 7.4.x).
Those suggested methods include:
- Using
Test-Path:
Test-Path -LiteralPath 'C:\Test\Some*File.txt' -IsValid
- Casting to
FileInfo and catching any exceptions:
[System.IO.FileInfo]$path = 'C:\Test\Some*File.txt'
- Casting to
DirectoryInfo and catching any exceptions:
[System.IO.DirectoryInfo]$path = 'C:\Test\Some*File.txt'
- Calling
[System.IO.Path]::GetFullPath() and catching any exceptions:
$path = [System.IO.Path]::GetFullPath('C:\Test\Some*File.txt')
These all work fine in Windows PowerShell, producing a $false result or throwing an exception, but in PowerShell 7, these methods just ignore illegal characters and other issues with the specified paths, carrying on as if nothing is wrong, and allowing invalid file paths through.
I can't just revert to using Windows PowerShell either, because the aforementioned third-party module isn't available for it.
Some comments on #8823 mention similar problems, but that issue has now been automatically closed with no resolution after nearly 5 years, so I'm opening a new issue as per the bot's instructions!
Expected behavior
PS> Test-Path -LiteralPath 'C:\Test\Some*File.txt' -IsValid
False
PS> [System.IO.FileInfo]$path = 'C:\Test\Some*File.txt'
Cannot convert value "C:\Test\Some*File.txt" to type "System.IO.FileInfo". Error: "Illegal characters in path."
At line:1 char:1
+ [System.IO.FileInfo]$path = 'C:\Test\Some*File.txt'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
PS> [System.IO.DirectoryInfo]$path = 'C:\Test\Some*File.txt'
Cannot convert value "C:\Test\Some*File.txt" to type "System.IO.DirectoryInfo". Error: "Illegal characters in path."
At line:1 char:1
+ [System.IO.DirectoryInfo]$path = 'C:\Test\Some*File.txt'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException
PS> $path = [System.IO.Path]::GetFullPath('C:\Test\Some*File.txt')
Exception calling "GetFullPath" with "1" argument(s): "Illegal characters in path."
At line:1 char:1
+ $path = [System.IO.Path]::GetFullPath('C:\Test\Some*File.txt')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException
Actual behavior
PS> Test-Path -LiteralPath 'C:\Test\Some*File.txt' -IsValid
True
PS> [System.IO.FileInfo]$path = 'C:\Test\Some*File.txt'
PS> $path.FullName
C:\Test\Some*File.txt
PS> [System.IO.DirectoryInfo]$path = 'C:\Test\Some*File.txt'
PS> $path.FullName
C:\Test\Some*File.txt
PS> $path = [System.IO.Path]::GetFullPath('C:\Test\Some*File.txt')
PS> $path.FullName
C:\Test\Some*File.txt
Error details
No response
Environment data
PS> $PSVersionTable
Name Value
---- -----
PSVersion 7.4.0
PSEdition Core
GitCommitId 7.4.0
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Prerequisites
Steps to reproduce
I have a PowerShell function which accepts a file path as input and performs a number of data retrieval/manipulation tasks, before eventually passing the data and the file path to a cmdlet in a third-party module, which transforms the data, creates the specified file, and populates it with the transformed data.
I need to be able to test whether the provided file path is valid, (even if it doesn't exist yet), before passing it to the third-party cmdlet, but none of the various validation methods I can find online seem to work as expected in current versions of PowerShell (7.3.x or 7.4.x).
Those suggested methods include:
Test-Path:FileInfoand catching any exceptions:DirectoryInfoand catching any exceptions:[System.IO.Path]::GetFullPath()and catching any exceptions:These all work fine in Windows PowerShell, producing a
$falseresult or throwing an exception, but in PowerShell 7, these methods just ignore illegal characters and other issues with the specified paths, carrying on as if nothing is wrong, and allowing invalid file paths through.I can't just revert to using Windows PowerShell either, because the aforementioned third-party module isn't available for it.
Some comments on #8823 mention similar problems, but that issue has now been automatically closed with no resolution after nearly 5 years, so I'm opening a new issue as per the bot's instructions!
Expected behavior
Actual behavior
Error details
No response
Environment data
Visuals
No response