Test-Path: We Goofed

Test-Path: We Goofed

  • Comments 9

Test-Path: We Goofed!
    (also known as a documentation errata…)

Test-Path is a very handy little cmdlet. You submit a path and it tells you (TRUE/FALSE) whether the elements of the path exist on the computer. You can use it to determine whether particular files or folders are present (or missing) or to look for a registry key that indicates that a program is installed, among so many other uses.

My favorite Test-Path command reminds me when I've forgotten to copy my Windows PowerShell profile to one of my test machines (test-path $profile).

Test-Path is so good that there was no need to exaggerate its abilities in the docs, but we did. The following false statement appears in the description of the Leaf value of the PathType parameter:

"An element that does not contain other elements, such as a file or registry entry."

That's just not true. File, yes; registry entry, no.

Test-Path can detect registry keys (the containers), but it cannot detect registry entries (sometimes called "values") or the data in an entry. If you try, it always returns FALSE.

' Testing a registry key
C:\PS> test-path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
' Testing a registry entry
C:\PS> test-path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ExecutionPolicy

' Finding the registry entry
C:\PS> get-itemproperty -name ExecutionPolicy -path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Micro
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds
PSChildName : Microsoft.PowerShell
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
ExecutionPolicy : RemoteSigned

(You're using TAB completion on these paths, right?)

I've corrected the docs and added an example demonstrating this limitation. You'll see it in the next release of Windows PowerShell.

I'm really grateful to the kind user who reported this error. If you find an error (or any other problem) with the docs, please report it using the instructions HERE First, I'll feel really contrite, and then I'll fix it, but I'll be eternally grateful.

June Blender [MSFT]
Senior Programming Writer
Windows PowerShell

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • PingBack from http://blogrssblog.info/windows-powershell-test-path-we-goofed/

  • Is this because the registry provider doesn't return values as "items" but instead as "properties" of the key they're in?

    Isn't there a way that we could have it both ways -- properties and items, so we could use get-content instead of get-item properties, and the Regitry provider's use in PowerShell would relate to the FileSystem in the same way as regedit relates it to explorer?

  • Is there anything that can test for the presence of a Registry value?

  • This might be a good place (as would the documentation) to describe a workaround. Is there a command that can test for the presence of a Registry value?

  • I'm very new to PowerShell so probably wrong, but could you possibly use:

    $key = "HKCU:\Software\Remedy\ARUser\Users"

    (Get-ItemProperty $key).USER1 -eq $null

    This would check that the USER1 Value name within the HKCU:\Software\Remedy\ARUser\Users key exists.

    If it does would return False, if not will return True.

  • Will it be possible to write a ps1 script that can detect when a usb flash drive is inserted and then copy the content to a spesific folder?

  • Is it possible to do a test-path on a remote machine's registry key?

  • Simonpa,

    Thanks that does the trick! you probably want to use -ne rather than -eq. if it equals null then the path has failed and will return true, sort of the opposite of what you are looking for but an easy change.


  • But let's not talk about Test-Path $null or Test-Path ""

    should return $true, instead we get errors

Page 1 of 1 (9 items)