Traditionally, mapping a shared folder on a network drive on a remote computer has been carried out on the local computer through the Windows File Explorer or the net use command in the CMD.
In this blog post, I explain how!
What are PSDrives?
In Powershell, you can use PSDrives to create temporary drives in the shell.
In this blog post, I explain the scenario if you want to map a UNC drive in the current session.
How to access a network folder using Powershell
Method #1 – Map a temporary network drive using PSDrives
You can browse a UNC path in Powershell to temporarily map a network drive in the current Powershell session using the PSDrive CMDLet. Note that this method is sessions specific, and the mapping will be lost when you close the Powershell session.
Use this command to mount the network path \\server\share to P:
New-PSDrive -Name P -PSProvider FileSystem -Root \\server\share
Method #2 – Create a persistent mapped network drive using PSDrives
Suppose you don’t want to map a network drive in the running Powershell session temporarily. In that case, you can also create a mapped drive after closing the current session with the Persistent parameter. This will be similar to mounting a network drive in Windows Explorer.
New-PSDrive -Name P -PSProvider FileSystem -Root \\server\share -Persist
Method #3 – Use the traditional Powershell CMDLet
The other method of working with network paths in Powershell is to use the regular Powershell CMDlets.
The following CMDlets work natively with UNC paths:
Copy-Item \\server\sharedfolder1\file.extension \\server\sharedfolder2\file.extension
Which method do you use for browsing and managing network paths in Powershell? Please leave a comment below!
- Copy a file to a new directory using Powershell
- How to change Powershell version for backward compatibility
- How to create a menu selection script in Powershell
- How to copy a file to the current user profile using Powershell
- How to check the execution policy in Powershell and change if required
You can also just use ‘set-location \\server\share’ (or ‘cd \\server\share’)