In working with PowerShell and the Windows file system, a cmdlet that provides many interesting possibilities and use cases is the New-PSDrive cmdlet. This post will consider the cmdlet, what it is, what it can do, and example use cases.
Table of Contents
What is New-PSDrive?
New-PSDrive is a command in PowerShell that enables users to create and manage both temporary and persistent drives in a Windows environment. It allows you to map network drives, interact with file system location drives, and even create links to registry keys. You can create both temporary and persistent mapped network drives and define the drive letter used.
Example:
New-PSDrive -Name "D" -PSProvider FileSystem -Root "C:\YourDirectory"
Why Would You Use the New-PSDrive Cmdlet and Use Cases
Let’s consider why you would use the New-PSDrive cmdlet and what use cases make sense to use it. Note the following examples:
Simplifying Access to Remote Resources
One of the primary advantages of the New-PSDrive cmdlet is its ability to simplify access to remote resources. The cmdlet provides seamless access, whether it’s a network share on a remote computer or a file system directory within the same network. This makes navigation and file management more efficient and ensures consistency across various local or remote locations.
Temporary and Persistent Drives Management
New-PSDrive offers robust management of both temporary and persistent drives. Temporary drives can be valuable for one-time tasks or temporary projects, existing only within the current PowerShell session.
On the other hand, persistent drives enable you to create permanent connections to important data store locations, ensuring they are readily available whenever needed. This flexibility makes it an indispensable tool for managing different types of drives and network connections.
Organizing Registry Keys and Other Data Stores
Beyond file system management, New-PSDrive extends its functionality to the Windows registry. System administrators can efficiently organize and access important registry data by mapping specific registry keys. This functionality also translates to better control over various data stores, making it an essential tool for managing complex environments.
Integrating with Windows Tools and Features
Integration with standard Windows tools like Windows Explorer, net use, and other features adds another layer of convenience to New-PSDrive. It enables users to create drives that are readily accessible through familiar interfaces. This seamless integration promotes ease of use and broadens the scope of tasks that can be performed using familiar Windows tools.
Windows Mapped Network Drive
Creating a Persistent Mapped Network Drive
Admins may need to work with a remote file system location on another server in the environment. The PowerShell New-PSDrive cmdlet allows creating persistent mapped network drive locations that will remain even after a restart.
Example of Windows mapped network drives:
New-PSDrive -Name "X" -PSProvider FileSystem -Root "\\NetworkPath\Share" -Persist
To include login details for authentication, you can do the following:
$cred = Get-Credential CLOUD\administrator
New-PSDrive -Name “X” -Root “\\<servername>\<share name” -Persist -PSProvider “FileSystem” -Credential $cred
You can get the details of the mapped PowerShell drives using the New-PSDrive cmdlet by using another related cmdlet, Get-PSDrive.
Working with Temporary Drives
Temporary drives exist only for the current PowerShell session. Here’s how to create a temporary drive using New-PSDrive:
Example:
New-PSDrive -Name "Y" -PSProvider FileSystem -Root "C:\TempFolder"
Working with FileSystem and Registry Key
FileSystem Provider and Roots
The PowerShell filesystem provider allows you to access file system locations like drives. You can create file system drives that target folders on your local directory, network share, or even an external drive.
Example:
New-PSDrive -Name "F" -PSProvider FileSystem -Root "C:\Program Files"
Working with Registry Key
You can create drives that point to registry keys using the New-PSDrive cmdlet. This can simplify accessing specific registry keys like the MyCompany registry key.
Example:
New-PSDrive -Name "HKMicrosoft" -PSProvider Registry -Root "HKEY_LOCAL_MACHINE\Software\Microsoft"
Advanced Usage of New-PSDrive
Using PowerShell Sessions and Windows Management Instrumentation
PowerShell sessions allow you to run commands on both local and remote computers. Windows Management Instrumentation (WMI) can be utilized alongside New-PSDrive for more advanced tasks.
Example:
New-PSDrive -Name "RemoteDrive" -PSProvider FileSystem -Root "\\RemoteComputer\Share" -Credential $credentials
You can create drives that map directly to UNC paths of network shares. This provides an easy way to interact with files on the same network share.
Example:
New-PSDrive -Name "UNCDrive" -PSProvider FileSystem -Root "\\Server\Share"
Frequently Asked Questions
What Differentiates a Temporary Drive from a Persistent Drive in PowerShell?
A temporary drive is used for the current session and is deleted once the session ends. It’s useful for temporary tasks.
Alternatively, a persistent drive is available across different PowerShell sessions, even after a reboot. It can be used to map network drives or access a remote resource in a persistent manner.
How Does New-PSDrive Interact with Standard Windows Tools?
New-PSDrive is a cmdlet that can also create drives that are accessible using Windows Explorer and other Windows features. It allows users to interact with drives using File Explorer to access data stored in different locations, including network shares or external drives.
Can I Use New-PSDrive to Access a Remote Computer or Different Computer on the Network?
Yes, New-PSDrive enables you to connect to remote computers or different computers on the same network share. By specifying the UNC path, you can create a mapped network drive to a specific network share’s UNC path, thereby accessing remote file system locations. This opens up numerous possibilities for sharing and managing files across diverse network environments.
How Do I Map Network Drives for Different User Accounts Using New-PSDrive?
You can map network drives that are specific to different user accounts. This allows you to tailor the drive’s access permissions based on the user name and their role. This function makes New-PSDrive particularly valuable for multi-user environments where different access levels to network drives might be needed.
Is the New-PSDrive Cmdlet Limited to the FileSystem Provider?
No, New-PSDrive can work with various providers, including the PowerShell registry provider. This extends its functionality beyond the file system to other areas like registry key management. It provides a unified way to access different types of data stores, making it a comprehensive solution for diverse system management needs.
Can I Create a Temporary Drive Mapped to a Local Directory on My Computer?
You can create a temporary drive mapped to a local computer’s directory. This can help organize your local directory structure and allows for temporary grouping or isolation of specific files or folders.
What Happens to Temporary PowerShell Drives When My Session Ends?
Temporary PowerShell drives, once created, exist only for the duration of the current PowerShell session. Once the session ends or if the user logs out, the temporary drives will be removed, and any associated data store with them will be inaccessible through that temporary drive path.
Wrapping up
New-PSDrive offers an extremely flexible way to interact with different types of data stores. The cmdlet provides many possibilities, whether you’re accessing a local or remote location, working with Windows Explorer, or dealing with a remote resource.
Understanding and using New-PSDrive in your PowerShell session not only simplifies many tasks but also opens up ways for advanced management of drives, including persistent and temporary drives, UNC paths, registry keys, and more.