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.
5 Answers
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 withSet-Location
to remote machine (i.e.servername
). Possibly this have something to do with UAC and or win10+ machines, but not sure. Sep 11, 2017 at 7:03
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.
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
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.
Adding to what others said about UNC paths in PowerShell, if the network share requires different credentials than you're logged into Windows with (like if it's on a domain that you're not on), it's been my experience that:
- You have to first browse the path once in File Explorer to get the credentials dialog. There's a "remember me" checkbox. Use Windows Credential Manager to change/delete remembered credentials.
- If the network share is on a domain you're not logged into, the username will need to be of the form
theDomain\theUserName
. - If you want access through administrator (aka elevated) PowerShell, you'll have to do the above through an elevated instance of File Explorer.
Set-Location \\SHARE-HOST\Share-folder
not work within Powershell for you? Is there an error message?net use * \\share\folder
will use the next free drive letter (starting fromZ:
)