Category filter

Script to Copy Files and Folders on Windows devices

Management becomes easier when data is stored systematically and can be retrieved easily. Most corporate devices contain sensitive data that need to be moved to secure locations frequently to prevent data breach.

Doing it manually might seem like the only option. But with Hexnode UEM, administrators can use custom scripts to copy files and folders to their Windows devices. Execute custom scripts on your corporate devices from remote locations using Hexnode’s custom scripting feature.

Note:


Supported Versions:

The scripts given below will be supported on the following versions:

  • Windows 10 v1607+ (Pro, Enterprise, Education)
  • Windows 11 (Pro, Enterprise, Education)

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Copy Files

PowerShell Script

1. Copy files if the file doesn’t exist in destination

The Copy-Item command is used to copy a file to another location. But, if the file already exists in the destination, the execution fails.

E.g., Copy-Item –Path “C:\Users\ABCD\Image.jpg” -Destination “C:\Users\ABCD\Pictures”.

The command copies the file “Image.jpg” to the folder “Pictures” if it does not already exist in the destination.

2. Copy files by overwriting the destination folder

The -Force command copies a file to the specified location and overwrites the file at the destination even if the file already exists.

Batch Script

Parameters
Parameter Description
[source] Specify the original location of the source file.
[destination] Specify the destination.
/d All the copied encrypted files will be saved as decrypted files at the destination.
/v Verify that new files are written correctly.
/n Uses a short file name, if available, when copying a file with a name longer than eight characters or with a file name extension longer than three characters.
/y Suppresses prompt- “Confirm to overwrite an existing destination file”.
/-y Prompts to confirm command to overwrite an existing destination file.
/z This utility will copy files in a restartable state, so they can be resumed after network outages.

The options [/d] [/v] [/n] [/y] [/-y] [/z] are provided as examples of possible switches. You should use only those options that apply to your needs. Do not attempt to use the complete option string; your command will generate an error.

When using these switches in your code, make sure to remove the square brackets. For example, enter /d /v /y /z instead of [/d][/v][/y][/z].

E.g., xcopy “C:\Users\QA\Program files“ “C:\Users\QA\Backup.” /d/v/n/y/z

Copy folder

PowerShell Script

The -Recurse parameter copies all the files and subfolders and their contents to the destination recursively.

E.g., Copy-Item -Path“E:\Hexnode” -Destination“D\: Backup” -Recurse.

All files and subfolders in the “Hexnode” folder will be copied to the destination.

Notes:

  • Remember to put a backslash after the folder name. Else, it will search for a file rather than a folder.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode UEM will not be responsible for any damage/loss to the system on the behaviour of the script.

Sample Script Repository