Issue with Remove-psdrive

Hello Powershell community.

I’m having an issue with a script.

I’m using it to send a file to a list of computers. everything works fine at the beggining but after a while i get this error :

New-PSDrive : The local device name has a remembered connection to another network resource

Here is my script

[pre]

Remove-PSDrive -Name k -ErrorAction silentlyContinue #in case there already is a K:
$liste = get-content "\\*****\3-Clients\3-Run\Manpower\scripts\listepchd.txt"
$fichier = "\\*****\3-Clients\3-Run\Manpower\TSD\MPW - Nettoyage disque .ps1"
$LWTSERVER = Get-childitem "\\*****-\3-Clients\3-Run\TSD\MPW - Nettoyage disque .ps1"
$liste| foreach {
$items = $_.split(",")
}
foreach ($item in $items){
Start-Sleep 3
[String]$path = "\\$item\c$"
[string] $destination = "k:\Temp\Nettoyage disque"
write-host "$item marche bien"
New-PSDrive –Name “K” –PSProvider FileSystem –Root “$path” –Persist
write-host "\\$path est le chemin"
if (!(test-path -path $destination)) {
New-Item $destination -ItemType directory
Copy-Item -path $fichier -Destination $destination -recurse -force
Remove-PSDrive -Name k -ErrorAction Continue
} else {
$presence = test-path -Path 'K:\Temp\Nettoyage disque\MPW - Nettoyage disque .ps1'
if ($presence -eq "true") {
$LWTPC = Get-ChildItem -Path "K:\Temp\Nettoyage disque\MPW - Nettoyage disque .ps1"
if ($lWTPC.lastwritetime -lt $LWTSERVER.lastwritetime){
Remove-Item -Path "k:\Temp\Nettoyage disque\MPW - Nettoyage disque .ps1"
Copy-Item -path $fichier -Destination $destination -recurse -force
Remove-PSDrive -Name k -ErrorAction Continue
}else{
write-host "fichier sur le poste, plus récent, pas de copie"
Remove-PSDrive -Name k -ErrorAction Continue
}
Remove-PSDrive -Name k -ErrorAction silentlyContinue
}
}
}
Remove-PSDrive -Name k -ErrorAction SilentlyContinue

[\pre]

It works for maybe 15 computers, and then i only get the error for every item.

I don’t know what to do, can someone give me a hint?

Thanks

My assumption is that a K is mapped and there is an active connection to files or something. When you do this:

Remove-PSDrive -Name k -ErrorAction silentlyContinue

It’s doing a SilentlyContinue, which means if this fails you are still trying to do the rest of your script, but having K is a requirement. While you can wrap a try\catch around this and use -ErrorAction Stop, the script is going to stop working for that machine. A better approach would not to depend on K: and use some logic to dynamically determine next available letter:

#Define drive letters we want to try
$DriveLetters = 69..90 | foreach{[char]$_}
#Get drives used on machine
$usedDrives = Get-PSDrive -PSProvider FileSystem | Select -ExpandProperty Name

#Use compare object to see what is available and return the letter we can use
$nextAvailDriveLetter = Compare-Object -ReferenceObject $usedDrives -DifferenceObject $DriveLetters |
                        Where-Object -FilterScript {$_.SideIndicator -eq '=>'} |
                        Select-Object -ExpandProperty InputObject -First 1

'Mapping to next available drive {0}' -f $nextAvailDriveLetter

For instance, on my machine, it’s E:

Mapping to next available drive E

If I map E, then it would move to F…if F was mapped then it would return G.

Thanks for the answer, i finally took it all back to the beginning and managed to find a much easier way, i just went with copy-item //remotecomputer and it worked perfectly fine.

I never needed to map a drive -.-

Sorry for taking your time and thanks again, i will use your solution later for sure =)