How to map drives and folders with Powershell?

How to map drives and folders with Powershell?

Shared folders and NAS are a common in organizations of all sizes, even at home these days. They are useful to IT support professionals, for example, to backup valuable information or to store applications and other files.

The first step is to confirm if the SMB V1/2 protocols are enabled. Open Powershell (v3 or higher) as administrator and run:

Get-SmbServerConfiguration | Select EnableSMB1Protocol, EnableSMB2Protocol

Shows if SMB v1/2/3 are enabled in Windows 10 and 8.1 or Server 2012/2016

If you need to map drives to older operating systems, enable SMB v1, open Powershell as administrator and run the command to install it:

Enable-WindowsOptionalFeature -Online -FeatureName smb1protocol

A computer restart is required!

Set-SmbServerConfiguration -EnableSMB1Protocol $True

This command will activate if it didn’t happen after the installation and restart

For SMB v2, its also necessary to run Powershell as administrator and run the command:

Set-SmbServerConfiguration -EnableSMB2Protocol $True

No restart required.

The command below shows the folders a computer is currently sharing with other users:

Get-SmbShare

In a way its the Powershell equivalent of “net share”

To view all the currently mapped drives in the current machine the command below is handy:

Get-PSDrive -PSProvider FileSystem

This one shows all the mapped drives

Get-SmbMapping

Alternative command to Get-PSDrive but without all the details

To add a new remote drive its necessary to keep in mind that doing it with elevated permissions (Run as admin.) might cause the drives to not be available to all users, the command is:

New-PSDrive -name M -psprovider FileSystem -root \\Server\Share -Persist -Scope Global

Replace M with an available drive letter, and the root is the remote server, share name and path

New-PSDrive -Name (68..90 | %{$L=[char]$_; if ((gdr).Name -notContains $L) {$L}})[0] -Root “ \\Server\Share” -PSProvider FileSystem -Persist -Scope Global

This command will map it to the first available drive letter

To create and share a new folder and add full access permission to a user, the script below helps, just replace the variables to suit your needs and run it:

To remove drives, the command below with the drive letter usually does the trick:

Remove-PSDrive -Name G -Force

Replace G for the requested drive letter