New SmbMapping - PowerShell


Overview

New-SmbMapping establishes a connection to an SMB (Server Message Block) share, creating a mapping between a local drive letter and the remote network path. It grants access to remote files and folders, simplifying management and collaboration.

Syntax

New-SmbMapping [-DriveLetter] <String> -RemotePath <String> [-Credential] <PSCredential> [-Confirm] [-Force] [-Session] <PSSession | String> [-Verbose] [-ErrorAction] <Stop | Continue | SilentlyContinue>

Options/Flags

| Option | Description | Default |
|—|—|—|
| -DriveLetter | Specifies the drive letter for the network share. | None |
| -RemotePath | The network path to the SMB share. | None |
| -Credential | Specifies the user credentials to access the SMB share. | Current user credentials |
| -Confirm | Prompts for confirmation before executing the command. | False |
| -Force | Overwrites existing drive mappings without confirmation. | False |
| -Session | Runs the command in the specified PowerShell session. | Current session |
| -Verbose | Displays detailed information about the command’s execution. | False |
| -ErrorAction | Specifies the action to take in case of errors. | Stop |

Examples

Example 1: Mapping a Network Drive

New-SmbMapping -DriveLetter Z -RemotePath \\server\share

Example 2: Using Alternate Credentials

$creds = Get-Credential
New-SmbMapping -DriveLetter S -RemotePath \\server\share2 -Credential $creds

Common Issues

| Issue | Solution |
|—|—|
| Drive letter already in use | Use the -Force parameter to overwrite the existing mapping. |
| Invalid remote path | Verify the network path and ensure it exists and is accessible. |

Integration

Example: Running a Script on a Mapped Drive

New-SmbMapping -DriveLetter E -RemotePath \\server\share
Get-ChildItem E:\ | Out-File E:\report.txt
  • Remove-SmbMapping
  • Get-SmbMapping
  • Net Use