2

I am trying to get folders and files from the root of a UNC path name. I am using Get-ChildItem.

I can get results from a subfolder via the UNC path, but not the root folder. If run the command Get-ChildItem \\sf1\user1 from the command line, results are returned.

    Directory: \\sf1\user1


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        11/14/2013   3:40 PM            1.ISGROUP

When I try to execute Get-ChildItem \\sf1 from the command line I get errors.

Get-ChildItem : Cannot find path '\\sf1' because it does not exist.
At line:1 char:1
+ Get-ChildItem \\sf1
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\sf1:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Any suggestions on how to get folders and files from the root of a UNC path?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kevin
  • 21
  • 1
  • 3

1 Answers1

5

As Mathias pointed out, a valid UNC path consists of at least server and share (\\servername\sharename), optionally subfolders too (\\servername\sharename\sub\folder). You cannot list files/folders directly from a server. If you want to enumerate shares on the remote server use net view \\servername (as Anthony Stringer mentioned). Note that this will list only visible shares. If you also want to list hidden shares (shares whose name ends with a $) you need to run net share on the server

Invoke-Command -Computer servername -ScriptBlock { & net share }

or use WMI

Get-WmiObject -Computer servername -Class Win32_Share
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328