Access network share without mapping drive letter (PowerShell)

239,355

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.

Share:
239,355

Related videos on Youtube

ServerAdminGuy45
Author by

ServerAdminGuy45

Updated on September 18, 2022

Comments

  • ServerAdminGuy45
    ServerAdminGuy45 almost 2 years

    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.

    • jscott
      jscott over 10 years
      Does the command Set-Location \\SHARE-HOST\Share-folder not work within Powershell for you? Is there an error message?
    • flafla709
      flafla709 over 7 years
      "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:)
    • Joshua Hanley
      Joshua Hanley over 7 years
      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/…
  • Petr Abdulin
    Petr Abdulin almost 7 years
    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.