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.
6 Answers
PowerShell fully supports UNC paths; you can use them everywhere a directory or file name would be expected:
Set-Location \\servername\sharenameGet-ChildItem \\servername\sharenameCopy-Item \\servername1\sharename1\filename.ext \\servername2\sharename2Remove-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-ChildItemunless you change your current location withSet-Locationto remote machine (i.e.servername). Possibly this have something to do with UAC and or win10+ machines, but not sure.pabdulin– pabdulin2017-09-11 07:03:03 +00:00Commented 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.
I know this is an older question, but I put together this small PS function to let me navigate to network locations:
#Network Change Directory function ncd { param( [Parameter(ValueFromPipeline=$true)] [string[]]$netLocation ) $netLocation = 'Microsoft.PowerShell.Core\FileSystem::'+$netlocation cd $ExecutionContext.InvokeCommand.ExpandString($netlocation) } Then I just type:
ncd \\servername\sharename$ and am given a prompt like
PS Microsoft.PowerShell.Core\FileSystem::\\Servername\ShareName$> You will still need access to said share.
Set-Location \\SHARE-HOST\Share-foldernot work within Powershell for you? Is there an error message?net use * \\share\folderwill use the next free drive letter (starting fromZ:)