New-PSDrive | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers New-PSDrive.

When to use New-PSDrive?

The New-PSDrive cmdlet 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.

Temporary drives exist only in the current PowerShell session and in sessions that you create in the current session. They 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.

Because temporary drives are known only to PowerShell, you cannot access them by using File Explorer, Windows Management Instrumentation (WMI), Component Object Model (COM), Microsoft .NET Framework, or with tools such as net use.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use New-PSDrive?

Create a temporary drive mapped to a network share:

New-PSDrive -Name “Public” -PSProvider “FileSystem” -Root “\\SCCMDC\Public”

New-PSDrive uses the -Name parameter to specify PowerShell drive named Public and the -PSProvider parameter to specify the PowerShell FileSystem provider.

The -Root parameter specifies the network share’s UNC path.

To view the contents from a PowerShell session: Get-ChildItem -Path Public:

Create a temporary drive mapped to a local directory: 

$parameters = @{
Name = “MyDocs”
PSProvider = “FileSystem”
Root = “C:\Users\Administrator.ITP\Documents”
Description = “Maps to my My Documents folder.”
}

New-PSDrive @parameters

The -Name parameter specifies the drive name, MyDocs. The -PSProvider parameter specifies the PowerShell FileSystem provider.

Root specifies the local computer’s directory. The -Description parameter describes the drive’s purpose.

New-PSDrive uses the * splatted parameters * to create the MyDocs drive.

To view the contents from a PowerShell session: Get-ChildItem -Path MyDocs:

* Splatting is a method of passing a collection of parameter values to a command as unit.

PowerShell associates each value in the collection with a command parameter. Splatted parameter values are stored in named splatting variables, which look like standard variables, but begin with an At symbol (@) instead of a dollar sign ($). The At symbol tells PowerShell that you are passing a collection of values, instead of a single value.

Splatting makes your commands shorter and easier to read. You can reuse the splatting values in different command calls and use splatting to pass parameter values from the $PSBoundParameters automatic variable to other scripts and functions.

Beginning in Windows PowerShell 3.0, you can also use splatting to represent all parameters of a command.

Create a temporary drive for a registry key:

New-PSDrive -Name “MyCompany” -PSProvider “Registry” -Root “HKLM:\Software”

New-PSDrive uses the -Name parameter to specify PowerShell drive named MyCompany and the -PSProvider parameter to specify the PowerShell Registry provider. The -Root parameter specifies the registry location.

To view the contents from a PowerShell session: Get-ChildItem -Path MyCompany:

Create a persistent mapped network drive using credentials: 

$cred = Get-Credential -Credential ITP\Administrator

New-PSDrive -Name “S” -Root “\\SCCMDC\Public” -Persist -PSProvider “FileSystem” -Credential $cred

Net Use

The $cred variable stores a PSCredential object that contains the Administrator account’s credentials. Get-Credential prompts you to enter the password that’s stored in a SecureString.

New-PSDrive creates the mapped network drive by using several parameters. -Name specifies the S drive letter that Windows accepts. and -Root defines \\SCCMDC\Public as the location on a remote computer. -Persist creates a Windows mapped network drive that’s saved on the local computer. -PSProvider specifies the FileSystem provider. -Credential uses the $cred variable to get the Administrator account credentials for authentication.

The mapped drive can be viewed on the local computer in PowerShell sessions, File Explorer, and with tools such as net use. To view the contents from a PowerShell session: Get-ChildItem -Path S:

 

 

 

Learn last week’s command: Get-ChildItem.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

Your email address will not be published. Required fields are marked *