Access network share without mapping drive letter (PowerShell)

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.


Solution 1:

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.

Solution 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.

Solution 3:

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

Solution 4:

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.