How to Map an SMB Drive Using PowerShell

  1. Introduction to the net use Command
  2. Mapping Network Drives net use Command
  3. Mapping Network Drives Using PowerShell
How to Map an SMB Drive 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:

  1. As an administrator, open a command prompt or a PowerShell console.

  2. Let’s map the F: drive to the C$ server file-sharing FS01. 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.

  3. Now run the net use command with no parameters to confirm Windows mapped the drive correctly.

    net use
    
  4. 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:

  1. LocalPath - the drive letter of the mapped drive.
  2. RemotePath - the UNC path of the remote file share.

Command:

New-SmbMapping -LocalPath 'F:' -RemotePath '\\FS01\c$'
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn