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 a 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.

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 Verifies 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.

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

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.

Batch Script

Parameters
Parameter Description
[source] Specify the original location of the source file.
[destination] Specify the destination.
/s Copies all the subdirectories, excluding the empty directories.
/e Copies all the subdirectories, including the empty directories.
/lev:[n] Copies only the top n levels of the source directory tree.
/move Moves files and directories, and deletes them from the source after copying them.

E.g.,robocopy “C:\Users\QA\Program files“ “C:\Users\QA\Backup” /lev3/move

Notes:

  • 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