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

Leave a Reply