How to transfer netdrives using PowerShell
Introduction
Every sysadmin knows the problem. An employee moves to a new workstation, notebook or takes over the duties of another employee. In all these cases, the need to take over the network drives of the old device or the colleague may arise.
In this tutorial you will learn how to transfer network drives from a source system / -profile to a target system / -profile in 2 simple steps.
Table of Content:
Prerequisites
- PowerShell 5.1 (tested version -> but I am sure it also works in other versions) - with Windows on board
- the access right to the desired SMB (Server Message Block) network shares
- the right to execute PowerShell scripts (see the next section)
Set the needed Execution Policy
- Select Start > All Programs > Windows PowerShell version > Windows PowerShell (as Administrator)
- Type Set-ExecutionPolicy Unrestricted to set the policy to Unrestricted.
- Type Get-ExecutionPolicy to verify the current settings for the execution policy.
- Type Exit.
Export
Below you will find the script that has to be executed.
$TxtPath = "c:\temp\MappedDrive.csv"
#Export network drives to file
Get-ChildItem "HKCU:Network\" |
ForEach-Object {
[PsCustomObject]@{
DriveLetter = $_.PSChildName
RemotePath = (Get-ItemProperty $_.PSPath).RemotePath
}
} | Export-Csv $TxtPath -Force
Below you will find the result -> a CSV file.
Import
Below you will find the script that has to be executed.
#Map network drives from file
$netdrives = Import-csv $TxtPath
#$netdrives
foreach ($drive in $netdrives){
#$drive.DriveLetter
#$drive.RemotePath
New-PSDrive -Persist -Name ($drive.DriveLetter) -PSProvider "FileSystem" -Root ($drive.RemotePath)
}
Enclosed is the displayed result:
Conclusion
In this short tutorial you learned how to migrate network drives easily.
If you want to learn more about the correct way to deal with network drives with PowerShell, then follow the link below:
And don't forget..... -> Share your knowledge.