20

I want to access a remote SMB network share \\SHARE-HOST\ without mapping a drive letter. I can manually do it in windows by typing \\SHARE-HOST\Share_folder\ in explorer. If I want to do it programatically I have to use the net use command. This requires me to specify a letter.

  • Does the command Set-Location \\SHARE-HOST\Share-folder not work within Powershell for you? Is there an error message? – jscott Oct 28 '13 at 1:58
  • 1
    "I have to use the net use command. This requires me to specify a letter." You don't have to specify one: net use * \\share\folder will use the next free drive letter (starting from Z:) – kapex Oct 6 '16 at 15:44
  • Actually, you don't have to specify or use a letter at all with net use. See the first answer here: serverfault.com/questions/580369/… – Joshua Hanley Oct 21 '16 at 14:15
22

PowerShell fully supports UNC paths; you can use them everywhere a directory or file name would be expected:

  • Set-Location \\servername\sharename
  • Get-ChildItem \\servername\sharename
  • Copy-Item \\servername1\sharename1\filename.ext \\servername2\sharename2
  • Remove-Item \\servername\sharename\foldername\filename.ext

However, you need proper access rights to actually connect to the remote share; this means either your user account must already have the required permissions, or you must manually establish a network connection to the remote server before accessing folders and files on it.

See also here: https://stackoverflow.com/questions/303045/connecting-to-a-network-folder-with-username-password-in-powershell.

  • Strangely enough, I've notived a behaviour where it is not possible to, for example, list content with Get-ChildItem unless you change your current location with Set-Location to remote machine (i.e. servername). Possibly this have something to do with UAC and or win10+ machines, but not sure. – Petr Abdulin Sep 11 '17 at 7:03
2

I am able to access a shared folder on my laptop by simply typing

cd \\MYLAPTOP\C$\Games

Use quotes if a folder name has spaces:

cd "\\MYLAPTOP\C$\Games\Game Name"

This gives me access to the file system of the remote computer.

PS Microsoft.PowerShell.Core\FileSystem::\\MYLAPTOP\C$\Games>

cd is an alias to Set-Location (s. http://technet.microsoft.com/en-us/library/ee176962.aspx) which supports such paths.

However, I required to log on to that computer in the explorer first by trying to open the path there. Then it worked in PS too. You probably need to add logon credentials in PowerShell first.

1

You need to “browse” the share, unless you have to authenticate with alternate credentials beforehand then you can use the below:

get-childitem \\server\share
1

just want to add to massimo's answer that I also noticed that if I was in other PSDrives I could not use network locations but regular locations work fine.

For example, if I am in Cert:\LocalMachine\> or PS HKLM:\> I cannot cd \\server\share

if I Set-Location C: and then Set-Location \\servername\share it works fine.

I also found that I didn't need to switch to the network location for many commands. For example I could Get-Content '\\127.0.0.1\C$\eula.1028.txt' from C:\ fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.