Как подключить сетевой диск с использованием power shell в windows 10

Windows 10 provides multiple ways to map a network drive on your computer, including using PowerShell, which can come in handy when creating a script or when you prefer using a command-line interface.

This guide will teach you how to use PowerShell to map a network drive on Windows 10. In addition, we will outline the steps to disconnect the mapping when it is no longer needed.

To map a network drive with PowerShell on Windows 10, use these steps:

  1. Open Start on Windows 10.

  2. Search for PowerShell and click the top result to open the console.

  3. New-PSDrive -Name "DRIVER-LETTER" -PSProvider "FileSystem" -Root "\\DEVICE-NAME-OR-IP\SHARED-FOLDER" -Persist
    New-PSDrive -Name "E" -PSProvider "FileSystem" -Root "\\vm-beta\ShareOne" -Persist

    PowerShell map network drive on Windows 10

Map network drive with credentials on PowerShell

To map a network drive providing the account name and password on Windows 11, use these steps:

  1. Search for PowerShell and click the top result to open the console.

  2. $cred = Get-Credential -Credential USERNAME

    Create credential variable with PowerShell

  3. Confirm your account password.

  4. Click the OK button.

  5. New-PSDrive -Name "E" -Root "\\DEVICE-NAME-OR-IP\SHARED-FOLDER" -Persist -PSProvider "FileSystem" -Credential $cred
    New-PSDrive -Name "E" -Root "\\vm-beta\ShareOne" -Persist -PSProvider "FileSystem" -Credential $cred

    PowerShell map network drive with password

Disconnect mapped network drive on PowerShell

To disconnect and remove a mapped network drive with PowerShell, use these steps:

  1. Search for PowerShell and click the top result to open the console.

  2. Get-PSDrive -PSProvider "FileSystem"
  3. Remove-PSDrive -Name DRIVE-LETTER

    In the command, replace “DRIVE-LETTER” with the drive letter of the mapping. For example, this command disconnects the “E” drive:

    Remove-PSDrive -Name E

    PowerShell disconnect map drive

  4. Get-PSDrive DRIVER-LETTER-1, DRIVE-LETTER-2 | Remove-PSDrive

    In the command, replace “DRIVER-LETTER-1” and “DRIVE-LETTER-2” with the drive letters you want to disconnect. For example, this command disconnects the “E” and “F” drives:

    Get-PSDrive E, F | Remove-PSDrive

Once you complete the steps, the drive mapping will be removed from the computer.

:/>  Windows 10 home / Домашняя ключ активации 2020-2021 - Лицензионные ключи для антивирусов и программ 2021-2022

Why You Can Trust Pureinfotech

I have a key-value pair collection in groovy, in a Jenkins pipeline e.g.

def procedureParameters = [:]
procedureParameters.put('key','value')

I’m trying to pass it to a powershell script –

def exitCode = powershell """ \$parameters = @{ ${procedureParameters} }
"""

The above is just an (not working) example, but in the end I want to have a hashtable in powershell that reflect the groovy map that I have.

I have no problems passing a single variable to powershell. But I couldn’t find a way to do that with a collection. Is there a good way to do that, before I start serializing my map to string and then deserializing it on the powershell side?

Palin Revno's user avatar

After much trial and error, here’s a working solution. It still requires to serialize the map into a string. But at least no further processing is required on the powershell side. That’s the groovy pipeline code :

def powershellInput = "@{"
procedureParameters.each { key, value -> powershellInput += "\"$key\"=\"$value\"; "
}
powershellInput += "}"
powershell
""" [HashTable]$parameters = ${powershellInput}
"""

Another option would be passing it to powershell in an environment variable. But I wasn’t able to make it work.

Palin Revno's user avatar

Palin Revno

4 silver badges11 bronze badges

Anthony M

Photo by Element5 Digital: https://www.pexels.com/photo/person-pointing-a-location-on-the-map-1051075/

In Windows 10, you can use PowerShell to remap a network drive. Below are the PowerShell commands to achieve this:

Explanation of the variables:

  • $DriveLetter: The desired drive letter for the network drive.
  • $NetworkPath: The full path to the shared folder.
  • $Username: Your username for accessing the shared folder.
  • $Password: Your password for accessing the shared folder.