Sven Kirsten
Sven Kirsten

Reputation: 518

Powershell Where-Object Match

I want to use the Where-Object to limit the Output of Get-PSDrive to only network shares.

Get-PSDrive shows me the following:

Name           Used (GB)     Free (GB) Provider      Root                                       CurrentLocation
----           ---------     --------- --------      ----                                       ---------------
A                                      FileSystem    A:\                                                       
Alias                                  Alias                                                                   
C                  16.19         43.47 FileSystem    C:\                                  Users\HansB\Documents
Cert                                   Certificate   \                                                         
D                                      FileSystem    D:\                                                       
Env                                    Environment                                                             
Function                               Function                                                                                                 
HKCU                                   Registry      HKEY_CURRENT_USER                                         
HKLM                                   Registry      HKEY_LOCAL_MACHINE                                        
V                 451.39        159.76 FileSystem    \\192.168.71.31\fs_log_target                             
Variable                               Variable                                                                
W                 197.72               FileSystem    \\192.168.71.32\perf200                                   
WSMan                                  WSMan                                                                   
X                 197.72               FileSystem    \\192.168.71.32\perf100                                   
Y                 271.52         34.33 FileSystem    \\192.168.71.30\group200                                  
Z                 271.52         34.33 FileSystem    \\192.168.71.30\group100                                  

Then I want to get the \\192.168.71.30\group100 Network Share:

Get-PSDrive | Where-Object  { $_.Root -match "\\\\192.168.71.30\\group100" }

But I get nothing, why does -match not work?

Upvotes: 1

Views: 2885

Answers (3)

Adam Mnich
Adam Mnich

Reputation: 471

Use DisplayRoot instead of Root property

Get-PSDrive | Where-Object  { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" }

try to run

Get-PSDrive | Where-Object  { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" } | select *

Root will be your mapped drive letter, and DisplayRoot your UNC Path

EDIT: as a side note. For escaping regex use [regex]::Escape() method.

PS > [regex]::Escape("\\192.168.71.30\group100")
\\\\192\.168\.71\.30\\group100

Upvotes: 6

Kjetil Mæland
Kjetil Mæland

Reputation: 39

If yoy type Get-PSDrive | select root you will notice that for network drives it will print nothing. So there is something strange with network share roots.

Get-PSDrive | select root | %{write-host $_.Root.GetType()} will show that it's a system string, so not quite sure why it seems to be empty for network drives.

Upvotes: 0

henrycarteruk
henrycarteruk

Reputation: 13237

Try using -eq instead:

Get-PSDrive | Where-Object {$_.Root -eq "\\192.168.71.30\group100"}

Upvotes: 0

Related Questions