Tuesday, October 16, 2018

New-PSDrive - PowerShell Cmdlet of the Week

To launch this series of exploring a PowerShell cmdlet each week, I'd like to start off with one I discovered last week.

New-PSDrive

I had need of this particular cmdlet as I was setting up a Hyper-V Server 2019 host and I needed to get the ISO images from a file share located on a remote file server called FILE01. Normally I would use the "Map Network Drive" utility built into File Explorer, but as there isn't a GUI to use, I needed a different solution. While I could have fallen back on the classic net use command, I thought it might be nice to figure out how to use PowerShell to accomplish this task.

As it turns out, it is really easy to connect to a remote file share. You simply enter:

New-PSDrive -Name Z -Root "\\file01\LabShare" -PSProvider Filesystem

To break down this list of Parameters:
-Name Z  -  Assigns the drive letter "Z"
-Root "\\file01\LabShare" - Specifies the remote server and share name
-PSProvider Filesystem  -  Tells the cmdlet that we want to use the filesystem provider

-Persist  -  Optionally, you can use this parameter to reconnect to the share at the next login

To confirm that we were successful, simply run Get-PSDrive













As you can see, we have successfully connected to our file share.

That's all there is to it!