rss logo

PowerShell script to copy network files to USB drive

PowerShell logo

In order to follow the 3-2-1 backup rule I recently developed a small PowerShell script to copy backup files (I used it for VEEAM but it works for any backup files) to a local USB drive. The goal is to make the copy accessible by any users.

The problem is as follows : copying backup files from a remote server to a USB drive connected to a local workstation that doesn't have pre-recorded credentials server.

Let's see, in details, how it works…

Get USB drive letter

The goal here is to automatically get the drive letter of our USB device. To do so, we will define a specific label for this drive and our script will get the letter associated to this label.

  • For example we can use USB_BACKUP label :
Windows | Explorer, USB_backup drive
  • This command will return the drive letter associated to the USB_BACKUP label :
PS C:\ > $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString() PowerShell | Get drive letter of USB drive

Network Credentials

Now we will generate a Window box thanks to Get-Credential command, to allow the user to enter the server credentials.

  • Get-Credential Window to get credential object and saves it in the $cred variable :
PS C:\ > $cred = Get-Credential PowerShell | Get-Credential
  • The net use command, used later accept clear password only, so we need to decrypt the password :
PS C:\ > $netcred = $cred.GetNetworkCredential() PS C:\ > $pass = $netcred.Password PowerShell | getnetworkcredential

Net use

We will use the net use command with the previously retrieved credentials in order to connect to our backup server.

  • Without the net use command, we cannot have access to the share without the need to authenticate :
Windows | enter network credentials window PS C:\ > net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
  • Once the net use command is entered we can access the share without the need to authenticate :
PowerShell | veeam backup share


The copy will be made by the robocopy command. Let's review the selected options.

PS C:\ > robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM"
  • Options :
    • /MIR : Mirrors a directory tree (equivalent to /e plus /purge).
    • /R:0 : 0 retry if fails
    • /W:0 : 0 wait time between retries
    • \\BACKUP_SERVER_IP\d$\Backup : source
    • "$usb_drive\VEEAM" : destination

PowerShell Script

Replace BACKUP_SERVER_IP with your current IP.

########################### # author : # version : 1.0 # date : 2021.03 # role : backup the backups to USB drive backup # other : Tested on Windows 2019 Server # updates : # - 1.0 (2021/03) : First Version #Get drive letter, quit if not found $usb_drive = try { $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString() } catch { exit 1 } #get credential window $cred = Get-Credential #get user and password credentials for net use command (see : $netcred = $cred.GetNetworkCredential() $pass = $netcred.Password #connect to the BACKUP_SERVER_IP network resource net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName) #mirror copy of files from "\\BACKUP_SERVER_IP\\d$\Backup" to "USB_DRIVE\VEEAM" folder robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM" #cancels the BACKUP_SERVER_IP network connection net use /delete \\BACKUP_SERVER_IP
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address