Test-Path: We Goofed

PowerShell Team

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
True
‘ Testing a registry entry
C:\PS> test-path HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ExecutionPolicy
False

‘ 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
soft.PowerShell
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

0 comments

Discussion is closed.

Feedback usabilla icon