New PSDrive - PowerShell


Overview

New-PSDrive creates a PowerShell drive that provides convenient access to a file system, registry, or other data source. It simplifies working with external resources by mapping them to a logical drive letter, enabling navigation and manipulation using standard PowerShell commands.

Syntax

New-PSDrive [-Name] <String> [-PSProvider] <String> [-Root] <String> [-Credential <PSCredential>]
                  [-Persist] [-Scope] <DriveScope> [-Verbose] [-WhatIf] [-Confirm]

Options/Flags

  • -Name: Specifies the name of the new drive.
  • -PSProvider: Indicates the provider to use for the drive. Common providers include “FileSystem”, “Registry”, and “Certificate”.
  • -Root: Sets the root path or location for the drive.
  • -Credential: Provides credentials for accessing the drive if necessary.
  • -Persist: Makes the drive persistent across PowerShell sessions.
  • -Scope: Defines the scope of the drive (“Global”, “Machine”, or “Session”).
  • -Verbose: Enables detailed output.
  • -WhatIf: Shows what the command would do without executing it.
  • -Confirm: Prompts for confirmation before executing the command.

Examples

Create a drive for a local folder:

New-PSDrive -Name MyFolder -PSProvider FileSystem -Root "C:\Users\John\Documents"

Map a drive to the system registry:

New-PSDrive -Name HKLM -PSProvider Registry -Root "HKLM:\Software"

Create a persistent drive with credentials:

$cred = Get-Credential
New-PSDrive -Name SecureFolder -PSProvider FileSystem -Root "C:\Confidential" -Credential $cred -Persist

Common Issues

  • Drive already exists: If a drive with the same name already exists, the command will fail. Use Get-PSDrive to check for existing drives.
  • Invalid path: Ensure the path specified in -Root is valid and accessible.
  • Access denied: Check if the credentials used have the necessary permissions to access the resource.

Integration

  • Use New-PSDrive with Get-Childitem to list the contents of a drive.
  • Combine with Set-Location to navigate to the drive.
  • Create scripts that automate tasks involving various data sources by leveraging different drive providers.
  • Get-PSDrive: Retrieves information about PowerShell drives.
  • Remove-PSDrive: Removes PowerShell drives.
  • Set-PSDrive: Modifies properties of existing drives.
  • Microsoft Docs: New-PSDrive