New-PSDrive

Create a temporary or persistent mapped network drive. (ndr, mount)

Syntax
      New-PSDrive [-name] string -PSProvider string [-Persist]
         [-root] string [-description string] [-scope string]
            [-credential PSCredential] [-WhatIf] [-confirm] [-UseTransaction] [CommonParameters]

Key
   -name 
       The PowerShell drive name to be added.
       For persistent mapped drives, use a drive letter e.g. 'h'.
       For temporary PowerShell drives, use any valid string e.g. 'SS64'.
       PowerShell drive names are case-sensitive though name comparisons are not case-sensitive.

   -description string
       A short description of the drive.

   -PSProvider 
       The name of the provider, if omitted you will be prompted.
       e.g. FileSystem, Registry or Certificate.
       Mapped network drives can be associated only with the FileSystem provider.
       type get-psprovider for a list of providers.

   -Persist
       Create a mapped network drive. Mapped network drives are persistent, not session-specific, and
       can be viewed and managed in File Explorer and other tools. (PowerShell 3.0+)

       The name of the drive must be a letter, such as D or E. The value of the Root parameter must
       be a UNC path to a different computer. The value of the -PSProvider parameter must be FileSystem.

       Mapped network drives are specific to a user account. Mapped drives created in elevated sessions
       or sessions using the credential of another user aren’t visible in sessions started using
       different credentials. [PowerShell 3.0+ Windows only]

   -root string
       The data store location that the PowerShell drive will be mapped to.

       e.g. a network share (\\Server64\files), a local directory (C:\Programs),
       or a registry key (HKLM:\Software\Microsoft).

       Temporary PowerShell drives can be associated with a local or remote location on any supported
       provider drive. Mapped network drives can be associated only with a file system location
       on a remote computer.

   -scope 
       A scope for the drive.
       Valid values are "Global", "Local", or "Script", or a number relative
       to the current scope (0 through the number of scopes, where 0 is the
       current scope and 1 is its parent).
       "Local" is the default. For more information, see help about_Scopes.

   -credential PSCredential
       A user account that has permission to perform this action.
       The default is the current user.
       a user-name, such as "User01" or "Domain01\User01", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any providers installed with PowerShell.

   -whatIf
       Describe what would happen if you executed the command without
       actually executing the command.

   -confirm
       Prompt for confirmation before executing the command.

   -UseTransaction
       Include the command in the active transaction.

Standard Aliases for New-PSDrive: mount, ndr

New-PSDrive creates temporary and persistent drives that are mapped to or associated with a location in a data store, such as a network drive, a directory on the local computer, or a registry key, and persistent Windows mapped network drives that are associated with a file system location on a remote computer.

Mapped drives added using New-PSDrive -persist will be available to other applications such as File Explorer immediately.

Performance: When mapping FileSystem network drives at login, performance can be important. Of the various methods available, New-PSDrive is faster than New-SmbMapping but still slower than NET USE. If the .Net framework has to be loaded, then just starting a Powershell process can take 2 to 3 seconds. In an already running PowerShell session, NET USE will still map a drive significantly faster, though with only the basic options available.

Elevation

An Elevated 'admin' process will not be able to see drives mapped under a limited user token and vice versa. This default behaviour can be reversed by setting EnableLinkedConnections, but few organisations will do that due to the potential security risks. Windows File Explorer runs as a non-elevated session.

Temporary drive Mapping

Temporary PS Drives (i.e. mapped without using the -Persist option) exist only in the current PowerShell session and for the duration of the current user session (until the user closes PowerShell or the drive is removed with Remove-PSDrive).

Temporary PS Drives are not visible to any other applications such as File Explorer, Windows Management Instrumentation (WMI), Component Object Model (COM), Microsoft .NET Framework, or to tools such as NET USE or Get-CIMinstance Win32_NetworkConnection

Temporary PS Drives can have any name that is valid in PowerShell and can be mapped to any local or remote resource. You can use temporary PowerShell drives to access data in the associated data store, just as you would do with any mapped network drive. You can change locations into the drive, by using Set-Location, and access the contents of the drive by using Get-Item or Get-ChildItem.

If a Temporary PS Drive is created with a name that is the same an an existing File System drive mapping e.g. H: mapped with NET USE, then it will temporarily replace that drive mapping within the PowerShell session. If the PSDrive is later removed then H: will automatically revert to its previous mapping.

Temporary PS Drives can be given a one letter name like traditional drive mappings, but there is no reason not to select a longer more descriptive name as they are not visible outside PowerShell anyway.

Temporary PS Drives are not inherited by any sub-shell or process that you open.

Persistent Drive Mapping

The following features were added to New-PSDrive in PowerShell 3.0:

Mapping Drives in a script

PSDrives are created in the scope in which the New-PSDrive command is run. When a New-PSDrive command is run within a script, the drive mapping is local to the script. To ensure that the drive is available after the script has completed, use the -Scope parameter and create the drive in the Global scope. Both Temporary and Persistent drives can be mapped using a script.

To disconnect a mapped network drive, use Remove-PSDrive. When you disconnect a mapped network drive, the mapping is permanently deleted from the computer, not just deleted from the current session. Mapped network drives are specific to a user account. Mapped network drives that you create in sessions that are started with the "Run as administrator" option or with the credential of another user are not visible in a session that started without explicit credentials or with the credentials of the current user.

When the value of the Root parameter is a UNC path, such as \\Server\Share, the credential specified in the value of the Credential parameter is used to create the PSDrive. Otherwise, Credential isn’t effective when you're creating new file system drives.

Examples

Install a drive called 'SS64' using the file system provider. The drive will be rooted at "C:\MyDocs":

PS C:\> new-psdrive -name SS64 -psProvider FileSystem -root C:\MyDocs -persist -scope global
DIR SS64:

Install a drive called 'S:' using the file system provider. The drive will be rooted at \\Server64\teams:

PS C:\> new-psdrive -name S -PsProvider FileSystem -root \\Server64\teams -persist -scope global
S:
PS S:\> DIR S:

The parameters are positional, so:

PS C:\> New-PSDrive -name M -psprovider FileSystem -root \\Server\Share

is the same as:
PS C:\> New-PSDrive M FileSystem \\Server\Share

Map a drive S: but only if the remote server is available:

$shared = "\\Server64\teams"

if (Test-Connection $shared -Quiet) {
  New-PSDrive -Name "S" -PSProvider FileSystem -Root $shared -Persist -scope global
}else{
  Remove-PsDrive -Name "S"
}

“If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside” ~ Robert X. Cringely, InfoWorld

Related PowerShell Cmdlets

New-SmbMapping - Map to an SMB share.
Get-PSDrive - Get drive information (DriveInfo).
Remove-PSDrive - Remove a provider/drive from its location.
Get-Command - Retrieve basic information about a command.
Get-Member - Enumerate the properties of an object.
VBScript MapDrive - Map a Drive letter to a network file share.
Equivalent bash command: mount - Mount a file system.


 
Copyright © 1999-2024 SS64.com
Some rights reserved