How to map network drives using powershell
Carter Sullivan
Published Mar 29, 2026
Mapped drives are the shares on remote computers for which you assigned a drive letter for easier access. We can query these drives and the target shares behind them with a simple and easy powershell one liner.
Here is the tip of the day. Happy Learning.
This is exactly what I needed in a pinch. Thanks for posting
Try this for Remote computers
$computername = Get-Content ‘I:\NodeList\SNL.txt’
$CSVpath = “C:\MSI\Mps.csv”
foreach ($computer in $computername) Write-host $computer
$colDrives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName $computer
foreach ($objDrive in $colDrives) # For each mapped drive – build a hash containing information
$hash = @ ComputerName = $computer
MappedLocation = $objDrive.ProviderName
DriveLetter = $objDrive.DeviceId
>
# Add the hash to a new object
$objDriveInfo = new-object PSObject -Property $hash
# Store our new object within the report array
$Report += $objDriveInfo
>
# Export our report array to CSV and store as our dynamic file name
$Report | Export-Csv -NoType $CSVpath #$filenamestring
>
Thanks for sharing the code. How sure are you that it works?
Mapped drives are specific to user login. You are referring to some other mappings here like drives mapped to folders?
Thanks for the code. I am having trouble getting it to run on workstations where I need it to run remotely. It will run well on any and all servers but not on any workstations. I am not sure why or what dependency is causing this. If I run the command on the workstation (Get-WmiObject Win32_MappedLogicalDisk -ComputerName Win10) it works as defined. Any clues?
Jake Gardner’s blog of information.
Scripting has changed. A new player has come into the works and added more functionality to the Microsoft scripting world, PowerShell.
You can map a network drive via script in a few different ways this post will show you how to do it using batch script, VBScript and PowerShell.
Map a network drive Batch script
Batch script is the most basic way to map a network drive with a script and is pretty easy to do.
net use F: \\fileserver\share /Persistant:yes
Now I will explain this the “net use” command it connects a Windows computer to a shared resource or shows information about shared resources. For more information go to your command prompt and type “net use /?” without the talking marks.
The “F:” part is allocating the drive letter, so when you go to My Computer it will be called drive F.
The “\\fileserver\share” part is specifying the full path to the network resource that you want to map, so in this example the hostname is “fileserver” and the shared folder is “share”
“/Persistan:yes” means that it is to stay there after a reboot. If you do not use the “/Persistant:yes” switch you may not have the shared folder there next time you go to access it.
Map a network drive VBScript
Once you move into VBScript you can add a bit more functionality like looking up groups or Organizational Units and then map different drives for users that are members of the different groups. A good example of this is on thinkpond.net. The basic command is:
WSHNetwork.MapNetworkDrive F: \\fileserver\share
“WHSNetwork.MapNetworkDrive” can be broken up into two parts. “WHSNetwork” which is an object for accessing shared network resources and “MapNetworkDrive” which is the command stating you want to map a network drive.
“F:” is specifying the drive letter
The “\\fileserver\share” part is once again specifying the full path to the network resource that you want to map the drive to.
Map a network drive PowerShell
With PowerShell you can add even more functionality than the VBScript within your scripting because of the power in PowerShell (yes thats right power in PowerShell 😉 ) you can do all the things that VBScript can do and more funky stuff like get network credentials automatically like Greg Caporale’s blog outlines.
New-PSDrive –Name “F” –PSProvider FileSystem –Root “\\fileserver\share” –Persist
“New-PSDrive” is a PowerShell cmdlet that is used for mapping network drives(Syntax available here)
“-Name “F”” is the part that specifies the network drive letter
“-PSProvider FileSystem” specifies the provider of the service which in this case is the FileSystem which is saying it is associated with a network share
“-Root “\\fileserver\share”” is saying where the network drive is going to be mapped to
“-Persist” this switch makes the drive persistent so after a reboot it will still be there
If you make a PowerShell script to do this and you want to run it you can run into some problems if you have not enabled PowerShell scripts to be able to run. To find out how to allow PowerShell scripts to run read this post.
Jake Gardner
Hi Everyone, I’m Jake. I have created this site basically as my own knowledge base, hopefully you find some of the information on here useful. I work with businesses across South West Victoria to leverage technology to improve the way they work & keep their data safe. If you want to connect with me look me up on LinkedIn.
You might also like …
How to Change Screen Resolution in Windows 10
How to Setup Outbound Spam notifications on Office 365 or Exchange
How To Recreate Arbitration Mailboxes – Exchange 2016
2 thoughts on “ Scripting a mapped network drive – Batch, VB and Powershell ”
Great post! I have one question. When I make the drive in PowerShell, why does it only show up after I reboot my computer. Net use shows up instantly, can PowerShell do that?
Superb post, but, say windows 7. I go into explorer “map network drive” “Drive letter” then “folder UNC path” then I check ‘Connect using different credentials’ a pop up “Windows Security” ‘User name’ – ‘Password’, My question is can either .vbs or powershell evn .bat get “Windows Security pop up” with these scripts ?
- ROM
- CPU
- RAM
- GPU
I am having difficulties mapping a network drive in Powershell. This script is meant to run at Windows Login. At the beginning of the script I have
Which removes the previously mapped drive. Then later in my script, I have the following to map the network drive
But the mapped drive does not show in Windows Explorer, and is not accessible by any of the local apps. Neither the station with the drive i’m mapping to or the station i’m running the script on are on a domain, the firewall on both stations is off, and I can map the drive manually in Windows. When I do a net use, it does not list the drive, but when I try remapping without removing the mapping it says the drive is in use.
Any thoughts or suggestions?
Using PS 5.1.17763.316 on both stations.
When you use the -Persist parameter to create a drive mapping (instead of a temporary PSDrive) you should specify the -Scope parameter as Global, otherwise the drive wont persist beyond the scope in which the command is running. If it’s running inside a script you should also dot-source the script and bear in mind I don’t believe you can use New-PSDrive if you already have an existing drive map to the same server using the same credentials. Some info from the help file:
Based on that I think you’d want your command to look like this:
11 Replies
PsDrives are not persistent. Use New-SmbMapping instead.
What is the ultimate goal here? Is this something that should be done in Powershell?
I have files on my network share that I copy down, and data is saved back to the network share as well. So most of the heavy lifting will be done outside of PS, i just need to establish the mapped drive. I’ve been using a batch file for years that has worked almost without flaw, but for security reasons I’d like to pass the credentials thru “silently” and encrypted thru PS.
Just curious, why not just use NET USE?
But the mapped drive does not show in Windows Explorer, and is not accessible by any of the local apps. Neither the station with the drive i’m mapping to or the station i’m running the script on are on a domain, the firewall on both stations is off, and I can map the drive manually in Windows. When I do a net use, it does not list the drive, but when I try remapping without removing the mapping it says the drive is in use.
Any thoughts or suggestions?
Using PS 5.1.17763.316 on both stations.
Is the script running as a local admin in both cases? Is UAC enabled?
UAC runs admin actions in a separate security context. It sounds like the script is running and mapping the drive correctly but in a different context where the user session does not see it. There is a registry key for merging the sessions. See the link below.
Question
Answers
Simply use ‘net use’ in your Powershell script:
net use x: \\server\share /persistent:yes
- Proposed as answer by Boe Prox MVP Thursday, September 8, 2011 11:57 AM
- Marked as answer by Richard Mueller MVP Monday, September 12, 2011 7:17 PM
All replies
Simply use ‘net use’ in your Powershell script:
net use x: \\server\share /persistent:yes
- Proposed as answer by Boe Prox MVP Thursday, September 8, 2011 11:57 AM
- Marked as answer by Richard Mueller MVP Monday, September 12, 2011 7:17 PM
I’ve never seen a PowerShell cmdlet for this. Net use seems the most direct. However, you can also use the wshNetwork object. For example;
$Network = New-Object -ComObject “Wscript.Network”
$Network.MapNetworkDrive(“P:”, “\\MyComputer\MyShare”)
Of course to make the mapping persistent, add the third parameter of the MapNetworkDrive function, $True, to the function.
The mapping only shows up in the PowerShell session.
Richard Mueller – MVP Directory Services
- Edited by Richard Mueller MVP Friday, September 9, 2011 12:39 AM add comment about 3rd parameter
Does it mean that there is no PowerShell command (cmdlet) to do this?
Why of course there is a PowerShell CmdLet to do this.
I will leave it up to you to insert the remainder of the help sections.
After loading by pasting or dot-sourcing type: Help Map-Adrive -full
You can also type:
Map-Adrive -?
help Map-Adrive -examples
- Edited by jrv Friday, September 9, 2011 1:06 AM
jv, I think that’s cheating! The OP said “Does it mean that there is no PowerShell command (cmdlet) to do this?”
I think he/she meant a built-in cmdlet in Powershell that can be used on any installation.
Has Powershell got a Show-Msgbox function? “Yes, I’ve got one for you” is not the right answer.
The right answer is, “No, but you can use a custom function to emulate it, but it will have to be loaded on each machine that you intend to run the script on.”
- Edited by Bigteddy Friday, September 9, 2011 6:36 AM
jv, I think that’s cheating! The OP said “Does it mean that there is no PowerShell command (cmdlet) to do this?”
I think he/she meant a built-in cmdlet in Powershell that can be used on any installation.
Has Powershell got a Show-Msgbox function? “Yes, I’ve got one for you” is not the right answer.
The right answer is, “No, but you can use a custom function to emulate it, but it will have to be loaded on each machine that you intend to run the script on.”
Awe come on – I was having fun.
The point is hthat PowerShell is extensible. I believe the PowerSHell designers asked the same question and purposely decided not to pu tin a legacy command.
Only legacy software requires drive mappings. It is a leftover from NetBIOS/NetBEUI. All modern software can diretly use a UNC. UNCs are preferred. drives are a remnant of the IBM ‘LU’ based IO model. You notice that we no longer print by using LPT1: LPT2:.
For the few times that we need to map a drive for the system it is easy enough to do with ‘NET USE’ don’t you think?
PSDrive is a convenience that is useful for shortening paths. Not that it works against the registry, SQLSercer, AD and any other place where a provider has been defined. It is kind of like teh old LU concept but specific to PowerShell and has no meaning outside of PowerShell. Of course that doesn’t meant that eventually teh PSDrivemodel won’t by extended outside of PoSH. Who knows?
A Windows PowerShell drive is a data store location that you can access like a file system drive in Windows PowerShell. The Windows PowerShell providers create some drives for you, such as the file system drives (including C: and D:), the registry drives (HKCU: and HKLM:), and the certificate drive (Cert:), and you can create your own Windows PowerShell drives. These drives are very useful, but they are available only within Windows PowerShell. You cannot access them by using other Windows tools, such as File Explorer or Cmd.exe.
Windows PowerShell uses the noun, PSDrive, for commands that work with Windows PowerShell drives. For a list of the Windows PowerShell drives in your Windows PowerShell session, use the Get-PSDrive cmdlet.
Although the drives in the display vary with the drives on your system, the listing will look similar to the output of the Get-PSDrive command shown above.
File system drives are a subset of the Windows PowerShell drives. You can identify the file system drives by the FileSystem entry in the Provider column. (The file system drives in Windows PowerShell are supported by the Windows PowerShell FileSystem provider.)
To see the syntax of the Get-PSDrive cmdlet, type a Get-Command command with the Syntax parameter:
The PSProvider parameter lets you display only the Windows PowerShell drives that are supported by a particular provider. For example, to display only the Windows PowerShell drives that are supported by the Windows PowerShell FileSystem provider, type a Get-PSDrive command with the PSProvider parameter and the FileSystem value:
To view the Windows PowerShell drives that represent registry hives, use the PSProvider parameter to display only the Windows PowerShell drives that are supported by the Windows PowerShell Registry provider:
You can also use the standard Location cmdlets with the Windows PowerShell drives:
Adding New Windows PowerShell Drives (New-PSDrive)
You can add your own Windows PowerShell drives by using the New-PSDrive command. To get the syntax for the New-PSDrive command, enter the Get-Command command with the Syntax parameter:
To create a new Windows PowerShell drive, you must supply three parameters:
A name for the drive (you can use any valid Windows PowerShell name)
The PSProvider (use “FileSystem” for file system locations and “Registry” for registry locations)
The root, that is, the path to the root of the new drive
For example, you can create a drive named “Office” that is mapped to the folder that contains the Microsoft Office applications on your computer, such as C:\Program Files\Microsoft Office\OFFICE11. To create the drive, type the following command:
In general, paths are not case-sensitive.
You refer to the new Windows PowerShell drive as you do all Windows PowerShell drives — by its name followed by a colon (:).
A Windows PowerShell drive can make many tasks much simpler. For example, some of the most important keys in the Windows registry have extremely long paths, making them cumbersome to access and difficult to remember. Critical configuration information resides under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion. To view and change items in the CurrentVersion registry key, you can create a Windows PowerShell drive that is rooted in that key by typing:
You can then change location to the cvkey: drive as you would any other drive:
The New-PsDrive cmdlet adds the new drive only to the current Windows PowerShell session. If you close the Windows PowerShell window, the new drive is lost. To save a Windows PowerShell drive, use the Export-Console cmdlet to export the current Windows PowerShell session, and then use the PowerShell.exe PSConsoleFile parameter to import it. Or, add the new drive to your Windows PowerShell profile.
Deleting Windows PowerShell Drives (Remove-PSDrive)
You can delete drives from Windows PowerShell by using the Remove-PSDrive cmdlet. The Remove-PSDrive cmdlet is easy to use; to delete a specific Windows PowerShell drive, you just supply the Windows PowerShell drive name.
For example, if you added the Office: Windows PowerShell drive, as shown in the New-PSDrive topic, you can delete it by typing:
To delete the cvkey: Windows PowerShell drive, also shown in the New-PSDrive topic, use the following command:
It’s easy to delete a Windows PowerShell drive, but you can’t delete it while you are in the drive. For example:
Knowledge Base Article
Quick Links and Search
How can we help?
Quick Login
This article applies to: Shared File Services
“Net use” is a command line method of mapping network drives to your local computer.
- The full syntax for net use is available from Microsoft.
- The Username and Password parameters are only required if the computer is not CornellAD joined.
Example: For CornellAD user “pqs665” to mount his CIT departmental CIFS share (“systems”) as drive letter H: on a CornellAD joined computer:
net use H: \\files.cornell.edu\cit\systems /persistent:yes
- Notes :
- “User” and “password” parameters are not required as user “pqs665” is authenticated from his logged in session on CornellAD.
- The “/persistent” flag enables the mapping to remain thru computer reboots.
- Quote characters (“\\files.cornell.edu\cit\systems files”) need to be added if the path has a space in it).
Example: For non-CornellAD user “PhilSchmertz” (local machine account, or account from an Active Directory domain other than CornellAD) to mount the same CIT departmental CIFS share (“systems”) as drive letter “H:”
net use H: \\files.cornell.edu\cit\systems /user:cornell\pqs665 * /persistent:yes
- Notes :
- This command prompts non-CornellAD user “Phil Schmertz” to enter his CornellAD “pqs665” domain password, then maps his departmental CIFS share as drive letter “H:”.
- The “*” forces the query for CornellAD password entry.
- The “/persistent” flag enables the mapping to remain until deliberately disconnected/deleted.
- Quote characters (“\\files.cornell.edu\cit\systems files”) need to be added if the path has a space in it).
To see what drives are mapped/available either look under “Computer” (or “My Computer”), or via command line enter Net Use .
To remove network mapped drives either select “Disconnect” (right-click on “Computer” or “My Computer”), or via command line enter: net use H: /delete
(H: in this example, only)
Table of Contents
Introduction
Do you have experience with mapping network drives using the command prompt but don’t understand how you access a network folder using Powershell?
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, the scenario that I explain is 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
One way you can browse a UNC path in Powershell is 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:
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.
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:
Conclusion
Which method do you use for browsing and managing network paths in Powershell? Please leave a comment below!
The help desk software for IT. Free.
Track users’ IT needs, easily, and with only the features you need.
I am having difficulties mapping a network drive in Powershell. This script is meant to run at Windows Login. At the beginning of the script I have
Which removes the previously mapped drive. Then later in my script, I have the following to map the network drive
But the mapped drive does not show in Windows Explorer, and is not accessible by any of the local apps. Neither the station with the drive i’m mapping to or the station i’m running the script on are on a domain, the firewall on both stations is off, and I can map the drive manually in Windows. When I do a net use, it does not list the drive, but when I try remapping without removing the mapping it says the drive is in use.
Any thoughts or suggestions?
Using PS 5.1.17763.316 on both stations.
When you use the -Persist parameter to create a drive mapping (instead of a temporary PSDrive) you should specify the -Scope parameter as Global, otherwise the drive wont persist beyond the scope in which the command is running. If it’s running inside a script you should also dot-source the script and bear in mind I don’t believe you can use New-PSDrive if you already have an existing drive map to the same server using the same credentials. Some info from the help file:
Based on that I think you’d want your command to look like this:
11 Replies
PsDrives are not persistent. Use New-SmbMapping instead.
What is the ultimate goal here? Is this something that should be done in Powershell?
I have files on my network share that I copy down, and data is saved back to the network share as well. So most of the heavy lifting will be done outside of PS, i just need to establish the mapped drive. I’ve been using a batch file for years that has worked almost without flaw, but for security reasons I’d like to pass the credentials thru “silently” and encrypted thru PS.
Just curious, why not just use NET USE?
But the mapped drive does not show in Windows Explorer, and is not accessible by any of the local apps. Neither the station with the drive i’m mapping to or the station i’m running the script on are on a domain, the firewall on both stations is off, and I can map the drive manually in Windows. When I do a net use, it does not list the drive, but when I try remapping without removing the mapping it says the drive is in use.
Any thoughts or suggestions?
Using PS 5.1.17763.316 on both stations.
Is the script running as a local admin in both cases? Is UAC enabled?
UAC runs admin actions in a separate security context. It sounds like the script is running and mapping the drive correctly but in a different context where the user session does not see it. There is a registry key for merging the sessions. See the link below.
This guide is part of a video series companion guide on setting up mapped drives on Intune devices – you can watch the video here S02E18 – How to Map Network Drives on Microsoft Intune Devices – (I.T) – YouTube! This is rather simple but I will be adding some useful bits of code for people who do not have an always on VPN solution for all those Work From Home scenarios.
Creating the script
Before we get started let me explain how this process works. We are going to create a script that we deploy via intune, which in turn will create a scheduled task to map the network drives at login. We will then be adding a few lines of code to also have it map on any network changes.
- Go to
- Follow the onscreen options to add/remove mapped drives as needed
- Select Download Powershell Script
- Edit the powershell script, near the bottom you will see the following line
$trigger = New-ScheduledTaskTrigger -AtLogOn - Remove everything below that point and add the following
Upload to Intune
- Navigate to
- Select Devices
- Select Scripts
- Select Add – Windows 10
- Give it a Name and select Next
- Select your script file and Next
- Assign to the desired user group and Next
- Select Add
Verify the Scheduled Task Exists
Once you have deployed the script to the selected group, you can sync Intune policies through Company Portal. Remember it can take up to 8 hours for this to appear. You should see a scheduled Task named IntuneDriveMapping and the triggers should look like the below.
The following two-step process will help circumvent the above-stated limitation.
1) CREATE HKEY_LOCAL_MACHINE\SOFTWARE\SCCMINVENTORY\NETWORKDRIVES PATH IN THE REGISTRY.
- Create an SCCM package.
- The package should run as an administrator.
- The package should run whether or not a user is logged on.
keywords in this post : network drive inventory, List all user network drives, SCCM Mapped drives
POWERSHELL PACKAGE 1 (Prerequisite):
- SAVE POWERSHELL FILE AS: NetworkDriveInvRegSetup.ps1
- SETUP THE SCCM PACKAGE/PROGRAM WITH COMMAND LINE:
2) CAPTURE CURRENT USER’S NETWORK DRIVES AND WRITE THOSE ENTRIES TO THE ABOVE CREATED REGISTRY KEYS.
- Create an SCCM package
- The package should be run only when a user is logged in.
POWERSHELL PACKAGE 2 (Main):
- SAVE POWERSHELL FILE AS NetworkDriveInventory.ps1
- SETUP THE SCCM PACKAGE/PROGRAM WITH COMMAND LINE:
3) CREATE A DEPLOYMENT AND SET IT TO ‘RUN ALWAYS’ AND MAKE IT A REQUIREMENT.
- Now deploy the second package and set the first package as a prerequisite (Check the box – Always run the prerequisite package)
- The deployment should be set to run every 4 hours and ‘Always rerun’. Mark the deployment as required.
4) ADD THE FOLLOWING IN BETWEEN THE EXTENSION SECTION WITHIN YOUR CONFIGURATION.MOF.
Click here to learn how to edit the configuration.mof file.
5) SAVE THE BELOW DATA INTO A FILE CALLED ‘AWESOME.MOF’. (Optional)
6) IMPORT ‘AWESOME.MOF’ INTO SCCM DEFAULT CLIENT SETTINGS.
- Either import the above MOF file into the Client Setting/Default Client Settings/Hardware Inventory/Classes/Import. Select the option to import everything.
- Alternatively, if you have compiled the MOF manually on the PC, Add a new reporting class by clicking the ‘Add’ button and connecting to the PC and selecting the WMI class ‘NETWORKDRIVES‘
and that is it. The SCCM resource explorer should soon see the Network drives. - You can then use the ‘NETWORKDRIVES‘ class to generate reports or create Queries.
keywords in this post : network drive inventory, List all user network drives, SCCM Mapped drives
Click here to learn more about network printer inventory.