0

I'm using Powershell 5 to search a folder for the most recent .bak file.

Get-ChildItem -Path $SourceBackupPath -Filter *.bak

This works fine if I pass in "C:\temp\" but if I pass in "E:\temp\", I get the error:

Get-ChildItem : Cannot find drive. A drive with the name 'E' does not exist.

If I pass in a UNC path "\\MyServer\e$\Temp\" I get the error:

Get-ChildItem : Cannot call method. The provider does not support the use of filters.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
J Brun
  • 1,246
  • 1
  • 12
  • 18
  • In your second test, the one with the UNC reference, it should be "\\", not "\" -- was that just a typo? – thepip3r Jun 01 '17 at 19:40
  • Thanks. Looks like stackoverflow converts 2 backslashes to 1. – J Brun Jun 01 '17 at 20:04
  • 1
    `Get-ChildItem -Path $SourceBackupPath | Where-Object { $_.Extension -eq '.bak' }` then sort by date, then take the most recent, is the most direct way to do it. But depending on number of files and folders to search, Get-ChildItem can also be slowwwww and you'd want to dig out to command prompt 'dir', Robocopy, Windows search indexer, 'everything' api, etc. to list the files more quickly. – TessellatingHeckler Jun 01 '17 at 20:04
  • So -Filter doesn't work on networked drives or UNC paths? That seems odd. And what if I want to search for *\*sometext\*.bak*? – J Brun Jun 01 '17 at 20:05
  • 1
    Get-ChildItem -Path $SourceBackupPath | ? { $_.Name -like "*sometext*.bak" } -- in this case, the asterisks are wildcards or if you're good with regular expressions: Get-ChildItem -Path $SourceBackupPath | ? { $_.Name -match 'somestring\.bak$' } – thepip3r Jun 01 '17 at 20:52
  • 1
    Curiously I cannot recreate the issue, I am able to get-childitem on any unc path with filters, No issues. `Get-ChildItem -Path "\\server01\Shared$\test" -Filter *.pdf` – Nick Jun 02 '17 at 05:22

1 Answers1

0

Are you using SQLPS by any chance? If so, check your working directory by looking at the command prompt. If it looks something like this:

PS SQLSERVER:\>

It means you you are issuing commands from the directory structure of the SQLPS provider, which doesn't recognize UNC paths by default. More info here.

Mike W
  • 83
  • 1
  • 7