rss logo

PowerShell Script for Copying Network Files to a USB Disk

PowerShell logo

In order to follow the 3-2-1 rule, I recently developed a small PowerShell script to copy backup files (I used it for VEEAM backups, but it works for any backup files) to a local USB disk. The aim is that the copy can be made by any user profile (from secretary to nuclear engineer).

The need is as follows: copy backup files from a remote server to a USB disk connected to a workstation that does not have pre-registered server credentials.

Let's see how it works in detail…

Get the USB drive letter

The aim here is to automatically retrieve the drive letter of our USB device. To do this, we'll define a specific label for this drive and our script will retrieve the letter associated to this label.

  • For example, we can use the label USB_BACKUP:
USB backup drive with available storage space displayed
  • 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 command to detect USB drive by volume name

Network Credentials

We're now going to generate a window box using the Get-Credential command, to allow the user to enter the server's credentials.

  • The Get-Credential command brings up a window that stores the following credentials in the $cred variable:
PS C:\ > $cred = Get-Credential Windows PowerShell credential request dialog box for user authentication
  • The net use command, used later, only the password in clear text, so we need to decrypt the password:
PS C:\ > $netcred = $cred.GetNetworkCredential() PS C:\ > $pass = $netcred.Password PowerShell script demonstrating how to get and use network credentials

The Net use command

We're going to use the net use command with the previously retrieved credentials to connect to our backup server.

  • Without the net use command, we can't access the share without authenticating ourselves:
Windows security prompt for network credentials with error message PS C:\ > net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
  • Once the net use command has been entered, we can access the share without having to authenticate ourselves:
Veeam backup folder showing full and incremental backup files

Robocopy

The copy will be performed by the robocopy command. Let's take a look at 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 : shebangthedolphins.net # 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, exit 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 : https://stackoverflow.com/questions/612015/copy-item-with-alternate-credentials) $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 the "\\BACKUP_SERVER_IP\\d$\Backup" folder to the "USB_DRIVE\VEEAM" folder robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM" #cancels network connection BACKUP_SERVER_IP 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