How do I add a network location using the CMD command line

kun.zhang 20 Reputation points
2023-12-29T06:45:38.1166667+00:00

How to add network locations via CMD command line instead of mapping network drives.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,722 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dikky Ryan Pratama 1,465 Reputation points
    2023-12-29T06:50:59.65+00:00

    Hi kun.zhang,
    To add a network location via the command line in Windows, you can use the net use command. However, keep in mind that the net use command is generally used for mapping network drives, not network locations. If you specifically want to add a network location that shows up in Windows Explorer under "Network Locations," you may need to use PowerShell instead.

    Here's how you can add a network location using PowerShell:

    
    # Replace "YourNetworkLocationName" and "\\Server\Share" with your desired values
    
    $locationName = "YourNetworkLocationName"
    
    $locationPath = "\\Server\Share"
    
    # Use New-Object to create a COM object representing the Shell
    
    $shell = New-Object -ComObject Shell.Application
    
    # Use the Shell object to create a shortcut for the network location
    
    $shortcut = $shell.CreateShortcut("$env:APPDATA\Microsoft\Windows\Network Shortcuts\$locationName.lnk")
    
    $shortcut.TargetPath = $locationPath
    
    $shortcut.Save()
    
    

    This PowerShell script creates a shortcut to the specified network location in the user's profile directory under "Network Shortcuts." The network location will then be accessible through Windows Explorer.

    Save the script with a ".ps1" extension and execute it in a PowerShell window. Make sure that you have the appropriate permissions to create a shortcut in the specified directory.

    Remember to replace "YourNetworkLocationName" with the desired name for the network location and "\Server\Share" with the actual network path you want to add.

    Please note that using PowerShell might be the closest alternative to achieving this programmatically. If you have specific requirements or constraints, please provide more details for a more tailored solution.

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    Regards.

    4 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.