Test-Path to check folder exits – strange results and the mysterious ‘x’

Welcome Forums General PowerShell Q&A Test-Path to check folder exits – strange results and the mysterious ‘x’

Viewing 2 reply threads
  • Author
    Posts
    • #197366
      Participant
      Topics: 3
      Replies: 2
      Points: 39
      Rank: Member

      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

       

    • #197375
      Participant
      Topics: 15
      Replies: 1761
      Points: 3,167
      Helping Hand
      Rank: Community Hero

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

    • #197381
      Participant
      Topics: 3
      Replies: 2
      Points: 39
      Rank: Member

      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.

Viewing 2 reply threads
  • The topic ‘Test-Path to check folder exits – strange results and the mysterious ‘x’’ is closed to new replies.