moswald
moswald

Reputation: 11677

Powershell: given a string, how can I tell what type of path I am dealing with?

For instance, I can type gci hkcu: and get a list of registry entries back, or I could type gci c:\ and get a directory listing.

Assuming $list holds the result of one of these queries, how can I tell what I'm dealing with?

I could, of course, just perform something like $list[0].GetType() and parse the result, but that's not very robust, and besides, what would I do with an empty list? (Which means I'm probably asking the wrong question, since I think I need to know the answer before I actually call gci.)

Upvotes: 2

Views: 163

Answers (3)

moswald
moswald

Reputation: 11677

While trying to code around the issue, I stumbled upon the answer:

(Get-ItemProperty $path).PSProvider.Name

This will return one of the providers listed in Get-PSProvider, and they are (typically) unchanging.

Upvotes: 4

manojlds
manojlds

Reputation: 301137

You can do the below maybe:

 $path = "HKCU:"
 $qualifier = (split-path $path -Qualifier).Trim(":")
 get-psdrive | ?{ $_.Name -eq $qualifier } | select provider

If the path is relative use resolve-path $path

Also you don't have to use gci on the path. you can just use gi:

(gi hkcu:).gettype()

Upvotes: 3

Guvante
Guvante

Reputation: 19203

The easiest way is to handle what is passed into gci, rather than attempting to handle the output. Then all you have to do is look before the :, or worst case check the "local" directory.

Upvotes: 0

Related Questions