Table of Contents
3.1. Screen shot
3.2. PowerShell Script
- End of Document
Purpose of this document is to explain how to copy a file from one location to another location via PowerShell where file name having date format.
This PowerShell script is created on request of one of my subscriber. Hope you will enjoy. If you have any query or concern please contact me at email@example.com.
- User wants to copy SQL backup file from one location (Ex: O:\Backup) to different network path \\ComputerName\SharedPath in Folder with current date.
- Only current dated file should be copied to destination.
- Once file copied user should receive file copied status (File name and Size)
## Owner : Niraj Srivastava ## Date : 11-Feb-2020 ## Version : 1.0 ## Copy daily backup files to different location cls $str = "*" * 130 $dt= (Get-Date).ToString("dd-MMM-yyyy") $fileDT=(Get-Date).ToString("yyyy_MM_dd") $hr= (get-date).hour $min = (get-date).minute $sec = (get-date).second $starttm = "$hr"+":"+"$min"+":"+"$sec" write $str Write "Copy Date :$dt time: $starttm $fileDT" # Change the source path from where you want to copy $sourPath="c:\Backup" # Provide IP or hostname where you want to copy the file $destPath="\\Shared_server\Backup_Repository" # Create folder at distination with current Date # Make sure the user executing the script have sufficient rights to create folder and folders at destination New-Item -Path "$destPath" -Name "$dt" -ItemType "directory" -ErrorAction SilentlyContinue $str Copy-Item $sourPath\NSITDB\NSITDB_$file_dt*.bak $destPath\$dt Copy-Item $sourPath\master\master_backup_$file_dt*.back $destPath\$dt Copy-Item $sourPath\model\model_$file_dt*.log $destPath\$dt # Send email about file copied at destination with file size # System should be able to reachanble to SMTP server $output=Get-Item $destPath\$dt\* | select Name, Length | ft Write "File copied to $destPath" write $output Send-MailMessage -To firstname.lastname@example.org -From email@example.com -SmtpServer nsitautomation.in -Subject "Backup Status $fileDT" -Body $output
PowerShell Script will create a folder with current date at destination and copy only latest files from source.
If it works for you please share, comment and like. Thank you.