Tag Archives: PSProvider

Quicktip: Get-PSDrive does not filter provider with Where-Object

When attempting to filter the results of Get-PSDrive you might notice that filtering by using Where-Object for the Provider property does not work as expected. For example the following code will not yield the expected results and will in fact not return any drives:

Get-PSDRive | Where-Object {$_.Provider -eq 'FileSystem'}

So let’s have a look at the reason for this, if we take a look at the Provider property of the output and what it contains:

PS C:\Users\JaapBrasser (Get-PSDrive).Provider.GetType()
 
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
 
PS C:\Users\JaapBrasser (Get-PSDrive).Provider | Get-Member -MemberType Property
 
 TypeName: System.Management.Automation.ProviderInfo
 
Name MemberType Definition
---- ---------- ----------
Capabilities Property System.Management.Automation.Provider.ProviderCapabil...
Description Property string Description {get;set;}
Drives Property System.Collections.ObjectModel.Collection[System.Management...
HelpFile Property string HelpFile {get;}
Home Property string Home {get;set;}
ImplementingType Property type ImplementingType {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
PSSnapIn Property System.Management.Automation.PSSnapInInfo PSSnapIn {get;}

So in order to correctly use Where-Object here, the comparison should be made to Provider.Name property instead, here is what the successful Where-Object filter script looks like:

Get-PSDRive | Where-Object {$_.Provider.Name -eq 'FileSystem'}

An easier and more correct form of only listing the FileSystem drives on your system would be by using the -PSProvider parameter of the Get-PSDrive cmdlet. By using this we can avoid using the pipeline and simplify the code even further:

Get-PSDRive -PSProvider FileSystem
Share

New article on PowerShell Magazine: Using UNC paths when working with PowerShell providers

The article explains two solutions to errors that you might run in to when utilizing UNC paths when working PowerShell providers. For example the execution of the following command will fail:

Set-Location HKLM:
Get-ChildItem | Export-Csv -Path \\localhost\c$\registry.csv

This can be solved by adding the specific provider, in this case the FileSystem PowerShell provider:

Get-ChildItem | Export-Csv -Path Microsoft.PowerShell.Core\FileSystem::\\localhost\c$\registry.csv

For more information on this topic head on over to PowerShell Magazine to have a look at the article:
http://www.powershellmagazine.com/2012/11/05/pstip-using-unc-paths-when-working-with-powershell-providers/

Share