Test-Path to check folder exits - strange results and the mysterious 'x'

I noticed that Test-Path has some apparent strange behaviour when checking if a folder exists. Basically, if the folder actually exists, Test-Path correctly identifies the folder for -PathType Container as one would hope. However, if I append an ‘x’ or ‘X’ to the end of the path where no such folder actually exists, Test-Path still returns True. This only seems to happen with the ‘x’ character (upper or lowercase). I wasn’t aware that ‘x’ was a special character? I haven’t seen any info to suggest it is. Maybe I’missing something. Please check the code below.

[string]$LogOutputFolder1 = "C:\Work\Automation" Test-Path -Path $LogOutputFolder1 -PathType Container #True Test-Path -Path $LogOutputFolder1 -PathType Leaf #False Test-Path -Path $LogOutputFolder1 -PathType Any #True

Folder/Path does not exist

[string]$LogOutputFolder1 = “C:\Work\Automationx”
Test-Path -Path $LogOutputFolder1 -PathType Container #True
Test-Path -Path $LogOutputFolder1 -PathType Leaf #False
Test-Path -Path $LogOutputFolder1 -PathType Any #True

Folder/Path does not exist

[string]$LogOutputFolder1 = “C:\Work\AutomationX”
Test-Path -Path $LogOutputFolder1 -PathType Container #True
Test-Path -Path $LogOutputFolder1 -PathType Leaf #False
Test-Path -Path $LogOutputFolder1 -PathType Any #True

Folder/Path does not exist

[string]$LogOutputFolder1 = “C:\Work\Automationy”
Test-Path -Path $LogOutputFolder1 -PathType Container #False
Test-Path -Path $LogOutputFolder1 -PathType Leaf #False
Test-Path -Path $LogOutputFolder1 -PathType Any #False

Folder/Path does not exist

[string]$LogOutputFolder1 = “C:\Work\Automationxx”
Test-Path -Path $LogOutputFolder1 -PathType Container #False
Test-Path -Path $LogOutputFolder1 -PathType Leaf #False
Test-Path -Path $LogOutputFolder1 -PathType Any #False


 

Mot sure what is happening, but I cannot reproduce what you are seeing:

PS C:\Users\rasim> Test-Path C:\Scripts
True

PS C:\Users\rasim> Test-Path C:\Scriptsx
False

PS C:\Users\rasim> Test-Path C:\ScriptX
False

PS C:\Users\rasim> Test-Path C:\ScriptX -PathType Container
False

PS C:\Users\rasim> Test-Path C:\ScriptX -PathType Any
False

PS C:\Users\rasim> $PSVersionTable

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      5.1.18362.145                                                                                                                                                                                                                     
PSEdition                      Desktop                                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                           
BuildVersion                   10.0.18362.145                                                                                                                                                                                                                    
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1      

I found the issue. Another Powershell script I was working on creates a log file and it was in that script I was using test-path to verify the log output folder… My own testing created the automationx folder! Oh boy. Anyway, thanks Rob.