Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering New-PSDrive this week.
When should you use New-PSDrive
The New-PSDrive cmdlet creates persistent and temporary drives that can be mapped to or associated to a location in a database store. This could include a directory on the local machine or a registry key. Persistent Windows mapped network drives that can be associated with a file location on a remote computer.
Temporary drives are only available in the current PowerShell session, and any sessions you create within the current session. They can be given any name that is valid for PowerShell and can map to any resource local or distant.
Temporary PowerShell drives can be used to access data stored in the associated data store just like you would with a mapped network drive. You can set the drive’s location by using Set-Location. To access the contents of the drive, use Get-Item and Get-ChildItem.
Temporary drives can only be accessed by PowerShell because they are only known to PowerShell.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.
How to Use New-PSDrive
Create a temporary drive that is mapped to a network sharing:
New-PSDrive -Name “Public” -PSProvider “FileSystem” -Root “\\SCCMDC\Public”
New-PSDrive uses -Name to specify the PowerShell drive Public and -PSProvider to specify the PowerShell fileSystem provider.
The -Root parameter indicates the UNC path for the network share.
To view the contents of a PowerShell session, go to Get-ChildItem-PathPublic:
Create a temporary drive that is mapped to a local directory
$parameters = @Name = “MyDocs”PSProvider = “FileSystem”Root = “C:\Users\Administrator.ITP\Documents”Description = “Maps to my My Documents folder.”
The -Name parameter specifies MyDocs’ drive name. The -PSProvider parameter indicates the PowerShell FileSystem provider.
Root is the directory of the local computer. The drive’s purpose is described by the -Description parameter.
New-PSDrive uses * splatted parameters* to create the MyDocs drive.
To view the contents of a PowerShell session, go to Get-ChildItem or MyDocs.
* Splatting allows you to pass a set of parameter values to a command unit.
PowerShell associates each value with a command parameter. Named splatting variables are used to store splatted parameter values. These look similar to standard variables but start with an At symbol (@), instead of a ($).. PowerShell is informed by the At symbol that you are passing multiple values and not one single value.
Splatting makes commands easier to understand and shorter. You can reuse the splatting value in multiple command calls. Additionally, you can use splatting for parameter values from the $PSBoundParameters variable to pass them to other scripts or functions.
You can use splatting to represent all parameters in Windows PowerShell 3.0.
For a registry key, create a temporary drive
New-PSDrive -Name “MyCompany” -PSProvider “Registry” -Root “HKLM:\Software”
New-PSDrive uses a -Name parameter for specifying the PowerShell drive MyCompany, and a -PSProvider parameter for specifying the PowerShell Registry provider. The registry location is specified by the -Root parameter.
To view the contents of a PowerShell session, go to Get-ChildItem or MyCompany.
Use credentials to create a persistent mapped network drive
$cred = Get-Credential -Credential ITP\Administrator
New-PSDrive -Name “S” -Root “\\SCCMDC\Public” -Persist -PSProvider “FileSystem” -Credential $cred
$cred stores the administrator account’s credentials in a PSCredential object. Get-Credential prompts for you to enter the password stored in a SecureString.
New-PSDrive creates a mapped network drive using several parameters. -Name is the letter of the S drive that Windows accepts. -Root is the location on a remote computer. SCCMDCPublic is defined by -Root. -Persist creates an Windows mapped network drive that can be saved on the local computer. -PSProvider is the FileSystem provider. -Credential uses $cred to obtain the Administrator account credentials for authentication.
The mapped drive can also be viewed on the local machine using PowerShell sessions, File Explorer and other tools like net use. You can view the contents of the mapped drive from PowerShell sessions by using Get-ChildItem and Path S: