How to Map an SMB Drive Using PowerShell
-
Introduction to the
net use
Command -
Mapping Network Drives
net use
Command - Mapping Network Drives Using PowerShell
One of the most reliable commands in a senior Windows administrator’s arsenal is the Windows net use
command, a legacy command to create and manage SMB Windows mapped connections and drives. We will learn about the legacy net use
command and its similar command counterpart in Windows PowerShell.
Introduction to the net use
Command
We can operate with files on network file shares using the net use
command. This is accomplished by using several parameters and switches, as illustrated below.
The net use
function allows us to view device connections, create new ones, and delete old ones. Throughout this article, we will learn about each parameter and observe how it applies to different scenarios.
Mapping Network Drives net use
Command
One of the net use
command uses to map network drives. This command allows us to map a network drive from the command line through the File Explorer.
Assume we are working on a Windows 10 PC in a domain with a Windows server that hosts a file share:
-
As an administrator, open a command prompt or a PowerShell console.
-
Let’s map the
F:
drive to theC$
server file-sharingFS01
. To do so, type the drive letter for the network drive, then the UNC path for the remote file share.net use F: \\FS01\c$
We can also use an asterisk (
*
) for the drive letter if we do not care which device to map the drive to. Doing so will tell Windows to find the following available drive letter.We can use any letter to map the network drive if it is not already used. If the file share has spaces in it, please surround it in quotes e.g.
-
Now run the
net use
command with no parameters to confirm Windows mapped the drive correctly.net use
-
Open File Explorer, and we will also notice a new network location.
Mapping Network Drives Using PowerShell
Alternatively, Microsoft has released a PowerShell cmdlet counterpart for the net use
command called New-SMBMapping
. The New-SMBMapping
command is similar to the net use
command, establishing an SMB mapping between the SMB client and an SMB share.
Like the net use
command, the New-SMBMapping
command accepts at least two parameters for a successful remote Mapping:
LocalPath
- the drive letter of the mapped drive.RemotePath
- the UNC path of the remote file share.
Command:
New-SmbMapping -LocalPath 'F:' -RemotePath '\\FS01\c$'
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn