Windows: Three ways to map a network drive using PowerShell

This subject is not directly related to virtualisation. However, it can be useful when you are not going to utilise Group Policy, and still need to automate drive mapping.

The old school

net use <drive_letter:> <UNC_path_to_the_network_drive> /persistent:[yes|no]

Pros: simple command that works.

Cons: not native to PowerShell; can be deprecated in the future.

Documentation: https://technet.microsoft.com/en-us/library/bb490717.aspx.

PowerShell 3.0+

New-PSDrive -Name <drive_name> -Root <UNC_path_to_the_network_drive> -PSProvider FileSystem -Scope [Global|Local] -Persist:[$true|$false]

New-PSDrive creates temporary and persistent mapped network drives. The scope should be set to allow other applications properly use mapped drives.

Pros: native to PowerShell.

Cons: require PowerShell 3.0+ to be fully functional.

Documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive.

PowerShell 5.0+

New-SmbMapping -LocalPath <drive_letter:> -RemotePath <UNC_path_to_the_network_drive> -Persistent:[$true|$false]

New-SmbMapping creates a Server Message Block (SMB) mapping on the SMB client to an SMB share.

Pros: native to PowerShell.

Cons: this cmdlet had some issues before PowerShell 5.0.

Documentation: https://docs.microsoft.com/en-us/powershell/module/smbshare/new-smbmapping.