How to change the Drive letter using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To change the drive letter using PowerShell, we can use the Set−Partition command but before that, we need to know which drive letter to change. You can check the drive letter using Windows Explorer, Get−Partition, Gwmi win32_Logicaldisk, or Get−CimInstance Win32_Logicaldisk command.

Suppose we have an E: and we need to rename its drive letter to F, so we can use the below command.

Set−Partition −DriveLetter 'E' −NewDriveLetter 'F'

Make sure that the drive is not in use by Pagefile, open application, or open file from the drive otherwise the drive letter will fail to change.

To change the drive letter on the remote computer, you can connect to a remote session using the New−CIMSession command or using the Invoke−Command remote computer method.

With the CIM session command,

$sess = New−CimSession −ComputerName Labmachine2k12
Set−Partition −CimSession $sess −DriveLetter 'E' −NewDriveLetter 'F'

In the above example, we are using a remote computer name LabMachine2k12. You can change as per your remote system.

With the Invoke−Command method,

Invoke−Command −ComputerName Labmachine2k12 −ScriptBlock{Set−Partition −DriveLetter 'E' −NewDriveLetter 'F'}

You can also use the DiskID instead of DriveLetter.

raja
Published on 25-Jan-2021 11:45:34
Advertisements