chaganaut
chaganaut

Reputation: 23

How to mount a SMB share to local folder - not only a drive letter - using powershell

Is it possible to mount a SMB share (or any other network share) to a local folder with powershell - apart from using "net use"? I tried New-SmbMapping and New-PSDrive. But I get errors for both. This is what I tried with New-SmbMapping

New-SmbMapping -LocalPath E:\MOUNT\pro\ -RemotePath \\host.example\projects

I get the specified device name is invalid and:

+ CategoryInfo          : NotSpecified: (MSFT_SmbMapping:ROOT/Microsoft/...MSFT_SmbMapping) [New-SmbMapping], CimException
+ FullyQualifiedErrorId : Windows System Error 1200,New-SmbMapping

And for New-PSDrive, it seems not possible at all.

Thanks for any help!

Upvotes: 0

Views: 3208

Answers (1)

Guss
Guss

Reputation: 32374

You cannot use New-SMBMapping to create a mapping to a local folder - only to a drive letter. The "device name is invalid" error refers to the fact that E:\MOUNT\pro is not a valid device letter (it expects a single English letter).

To mount an SMB path into a local folder, you need to create a "junction point" - what Powershell calls SymbolicLink. For example:

New-Item -ItemType SymbolicLink -Path mytest -Target '\\computer\share'

The local path (./mytest in the above example) must not exist as New-Item needs to create it (this is unlike file system mapping in other operating systems).

Your current user must have access to the remote share, otherwise you'd get a non-helpful error message such as New-Item : Cannot find path '\\computer\share' because it does not exist. You can access shares for which you need to provide a user name and password by first loading the credential with net use before running New-Item (I don't know of Powershell way to do net use).

Upvotes: 2

Related Questions