jeiea
jeiea

Reputation: 2405

How can I determine whether a path is filesystem or registry?

I'm making a function which can take a argument which can be either filesystem or registry path. e.g.

'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
'C:\ProgramData\Microsoft\Windows'

I don't want to divide them by named argument but their interfaces aren't compatible. How can I classify them?

Upvotes: 0

Views: 122

Answers (2)

TechSpud
TechSpud

Reputation: 3528

These commands tell you the types:

(Get-Item 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run').GetType().Name # returns RegistryKey
(Get-Item 'C:\ProgramData\Microsoft\Windows').GetType().Name # returns DirectoryInfo

...or another way of getting the same info...

$item = Get-Item 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
$item.GetType().Name # returns RegistryKey

$item = Get-Item 'C:\ProgramData\Microsoft\Windows'
$item.GetType().Name # returns DirectoryInfo

Upvotes: 0

user4003407
user4003407

Reputation: 22132

You can use this method ($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath) to do that. It have overload, which allows you to extract PowerShell provider and PowerShell drive info from path.

'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run',
'C:\ProgramData\Microsoft\Windows' |
ForEach-Object { $Provider = $null } {
    [void]$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_, [ref]$Provider, [ref]$null)
    $Provider
} 

Upvotes: 1

Related Questions