PowerShell Test-Path UNC with a wildcard

1.4k Views Asked by At

I'm trying to test if a UNC path exists, but all attempts have failed so far. This folder example exists, and returns true:

\\Server\Path1

I'd like to confirm all folders with similar names exist, such as:

\\Server\Path2 
\\Server\Path3 etc.

I've tried using a wildcard in these examples:

test-path "\\Server\Path*"
resolve-path "\\Server\Path*"
[System.IO.Directory]::Exists('\\Server\Path*');
Test-Path $('filesystem::\\Server\Path*')

...Along with many permutations of the \ " ' * combination. However, nothing I've tried returns a 'True' for this type of path when using a wildcard, even though it seems to work fine for: Test-Path c:\windows\system3* for example.

2

There are 2 best solutions below

1
On BEST ANSWER

I don't think Windows supports wildcard selection on share names.

But if you have sufficient (remote) access to the file server you can get a list of shares:

Get-WmiObject -class 'Win32_Share' -ComputerName 'Server'
0
On

This snippet will work for mapped UNC paths :

   Get-PSDrive| where{$_.DisplayRoot -like "\\server\test*" } | foreach{test-path -path $_.DisplayRoot}

If you have wmi access ,then :

Get-WmiObject -Class Win32_Share -ComputerName server | where{$_.name -like "test*"} | foreach{Test-Path "\\server\$($_.name)"}