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.
Copy Files
PowerShell Script
1. Copy files if the file doesn’t exist in destination
|
1 |
Copy-Item -Path[path to be copied from] -Destination[Path to be copied to] |
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
|
1 |
Copy-Item -Path[path to be copied from] -Destination[Path to be copied to] -Force. |
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
|
1 |
xcopy [source][destination][/d][/v][/n][/y][/-y][/z] |
| 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
|
1 |
Copy-Item -Path[path of folder] -Destination[path of destination folder] -Recurse. |
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.