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.