Understanding and Building New PS Drives in PowerShell

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

PowerShell is a task-based command line shell and scripting language environment for Microsoft Windows, developed by Microsoft. It was released in 2006 with much of the development being done in conjunction with the .NET Framework 3.0.

The “remove-psdrive” is a PowerShell command that allows users to remove drives from the PowerShell environment. It is a useful tool for building new PS Drives in PowerShell.

Understanding and Building New PS Drives in PowerShell

PowerShell drives are essential whether you’re working with a file system, registry, certificates, or even network drives with PowerShell. PowerShell drives, also known as PS drives, are a notion in PowerShell that enables you to interact with structured data in the same way that you would with a file system. The New-PSDrive cmdlet is a good place to start.

This lesson will teach you how PS drives function, how to utilize some of the cmdlets that interact with PS drives, and how to make your scripting chores simpler.

Let’s get started by opening a PowerShell console.

Prerequisites

This will be a hands-on presentation in this course. If you want to follow along, make sure you have the following items on hand:

  • A computer that runs on Windows — Despite the fact that PS drives will operate with PowerShell on other systems, this tutorial’s examples will all be done on Windows.
  • If you want to follow along with one of the demonstrations, you’ll need an accessible SMB network share.

Using Get-PSDrive to inspect PS drives

PowerShell comes with a few default drives installed out of the box. Run the Get-PSDrive command to view these drives. PowerShell will provide a list of drives, including file system drives like C and registry drives like HKLM and HKCU.

Show a list of all PS Drives that your PowerShell session has access to.Show a list of all PS Drives that your PowerShell session has access to.

The kind of data on PS drives varies depending on the supplier. PowerShell utilizes these providers to expose the data stored on each of the disks. Get-PSDrive has returned drives from eight different suppliers, as you can see above.

Get-PSProvider will show you all of the providers installed on your PC. As you can see in the diagram below, each providers have distinct capabilities that dictate how they interact with the current PowerShell session.

Using Get-PSProvider to locate PSProvidersUsing Get-PSProvider to locate PSProviders

There are a few useful arguments in Get-PSDrive, such as the PSProvider parameter. You may use this option to filter the PS drives returned by a certain provider. Get-PSDrive only returns drives that are serviced by the FileSystem provider, as seen below.

FileSystemGet-PSDrive -PSProvider Get-PSDrive -PSProvider Get-PSDrive -PSProvid

Limit the output of Get-PSDrive to Provider TypeLimit the output of Get-PSDrive to Provider Type

Getting Around PS Drives

After you’ve determined which PS drives are accessible, you may investigate what’s within each of them. The Env drive, for example, is a standard PS drive that stores Windows’ environment variables. Environment variables may be browsed in the same way that they can be browsed in a file system.

To explore the Env drive, use cd or Set-Location to go to the drive letter, then use ls or Get-ChildItem to browse it, as shown below.

Get-ChildItem: Listing Files, Registry, and Certificates is related to Get-ChildItem: Listing Files, Registry, and Certificates.

The colon at the end of each sample should be noted. To indicate a PS drive, you must use a colon.

When you execute ls, PowerShell provides key/value pairs for each environment variable in Windows, as seen below.

Env PS Drive InspectionEnv PS Drive Inspection

Using New-PSDrive to Create New PS Drives

You’ve seen that PowerShell comes with a few built-in PS drives, but you can also use the New-PSDrive cmdlet to construct custom drives.

The FileSystem provider is one of the most frequent ways to utilize the New-PSDrive cmdlet to build “shortcuts” to file system folders or even map network drives. Labels may be assigned to different file system locations using New-PSDrive.

Using New-PSDrive to Map a File Share

For instance, suppose you have an SMB file share on your network that has to be mapped to a drive letter. You may map the network drive using the GUI, or if you prefer the command line or are writing a script, you can use PowerShell.

Let’s pretend you have a network share named localhostPSDRIVE with the UNC path localhostPSDRIVE.

Only use the local computer (localhost) as an example. In the same manner, you may map real network disks using New-PSDrive.

To use New-PSDrive to map a network drive, provide the following information:

  • The name of the drive that you want to create (NetShare in this example).
  • The kind of provider that the drive will utilize (PSProvider) (FileSystem in this example).
  • Using the Root argument, provide the path to the file share or localhostPSDRIVE.

To check what’s in the file share, execute New-PSDrive and then Get-ChildItem, as shown below.

Get-ChildItem -Path NetShare:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

You’ll see that referring to NetShare now has the same effect as referring to localhostPSDRIVE directly.

Using New-PSDrive to map a network driveUsing New-PSDrive to map a network drive

Do you need to add any notes on your new hard drive? Is it possible to use the file share to store documents? -Description ‘The file share to store documentation’ is an example of a description argument.

For the Windows GUI, a Filesystem PS Drive is used.

The PS drive is only visible in PowerShell in the example above. Using New-PSDrive in this method does not map a network drive in the same way that the GUI does. However, by adding the Persist argument, you may still do it.

New-PSDrive is told to expose the disk to Windows but not to PowerShell via the Persist options. You may, for example, use the command below to map an X drive to the WDMYCLOUDGARRY file share.

-Name “X” -PSProvider “FileSystem” -Root “WDMYCLOUDGARRY” -Persist New-PSDrive -Name “X” -PSProvider “FileSystem” -Root “WDMYCLOUDGARRY”

The X drive will now appear in Windows File Explorer.

The mapped disk in File Explorer is visible.The mapped disk in File Explorer is visible.

Creating PS Drives for Long Directory Paths in the Filesystem

Aside from mapping file shares, the New-PSDrive cmdlet may also be used to build PS drives to save time typing! You can get away with modifying the directory C:thisisareallylongdirectorypathIdonotwanttotype to just C:LongDirPath by establishing a disk based on a long directory path.

Microsoft SQL Server2019 saves its logs by default at the C:Program FilesMicrosoft SQL ServerMSSQL15.SQL2019MSSQLLog directory, as an example. It would get boring quickly putting that in every time you needed to go to that location in PowerShell.

Create a PS drive named sqllog instead than filling in a lengthy directory path using New-PSDrive. After that, modify the directory path to the PS drive name instead of the original directory path.

sqlLog -PSProvider -New-PSDrive “-Root “C:Program Files” -FileSystem MSSQL15.SQL2019MSSQLogMSSQLogMSSQLogMSSQLogMSSQLogMSSQLogMS “sqlLog: Set-Location

Creating a directory "shortcut" with New-PSDriveUsing New-PSDrive to create a directory “shortcut”

Collaboration with the Registry

PS drives aren’t simply for the file system anymore. Keep in mind that PS drives employ a variety of providers. The register is one of these service providers. You may make “shortcuts” to any registry entry you like using the same approach you taught.

Pass the Registry argument to the PSProvider parameter to construct PS drives on a registry provider, and instead of a directory path, use a registry key path for the Root, as seen below. It’s worth noting that this example also utilized the optional Description argument to give the PS drive a name.

-PSProvider “Registry” -Root “HKLMSoftware” -Description “my local Registry key” New-PSDrive -Name “myRegistry” -PSProvider “Registry” -Root “HKLMSoftware”

Run Get-PSDrive to check your work after it’s been generated. The Format-Table cmdlet is used in the example below to restrict output to only the Name and Description attributes. By default, the Description attribute is hidden.

Example of a PS Drive with a DescriptionExample of a PS Drive with a Description

Remove-PSDrive is a command for removing PS drives.

Finally, maybe you’re done with PS drives and it’s time to tidy up. Use the Remove-PSDrive cmdlet to do this. The PowerShell pipeline is one of the most straightforward methods to utilize the Remove-PSDrive cmdlet. You may delete many PS drives at once by “piping” them to Remove-PSDrive.

Perhaps you’ve completed all of the examples in this tutorial and now want to delete them altogether. Instead of executing Remove-PSDrive three times, use Get-PSDrive to read them all and pipe each drive to Remove-PSDrive, as demonstrated below.

Remove-PSDrive -Force -Scope Global | Get-PSDrive -Name NetShare, sqlLog, X

Each PS drive was already established, but performing the above command has erased them all.

It’s worth noting that the Force and Scope parameters aren’t required. The Force argument removes a PS drive even if a file is open, however the Scope parameter with the New-PSDrive -Scope parameter removes drives specified in the global scope.

Remove-PSDrive is a good example.Remove-PSDrive is a good example.

Conclusion

PS drives may help you save time by making it simpler to browse the file system, registry, and other data sources. Have you been compelled to include PS drives into your scripts? If so, how would you go about doing it?

The “new-psdrive active directory” is a PowerShell command that allows you to create and manage new drives within Active Directory.

Frequently Asked Questions

Related Tags

  • get-psdrive
  • new-psdrive not showing in explorer
  • new-psdrive scope
  • new-psdrive persist
  • new-psdrive : the network resource type is not correct