Main navigation

Transfer and Compare Size from different folders with Powershell

Powershell is a great way to automate your daily tasks.As IT Pro has a lot of tasks that must complete daily and more tasks to schedule it for next day , next month or repeat a tasks every month.

The necessary of automation it's important for every IT Pro. Sometimes the difficult is to find the time and the concentration that need to create a Powershell Script , do the necessary tests to automate one of your tasks.

After several weeks i would like to publish a new Powershell script that create to automate one of my task.

The reason that create the Powershell script was because every month i must copy-paste files from 3 Servers in one of my NAS , compare it if the source-destination has the same size , delete the files from the source and keep the date which transfer the files.

This tasks there should be done after working hours to avoid performance issues in the specific servers.

So let's start !!

Before start to explain the Powershell script i would like to explain which are the steps that do every month before create the script.

Stop the specific Service.

Copy-Paste folders/files from the Server to a NAS which need credentials to login

After finish the transfer compare the source and the destination if has the same size

Very important to create a file which log what happened. So you would know if the transfer complete or not

Step 3: Create Log File

$logProgress = 'c:\copyLogs.txt'

Step 3 :Copy Folder

With Copy-Item start the Copy from Source Folder to the Destination. As you can see the Destination is w:\$((Get-Date).ToString('yyyy-MM-dd')). The w is the Name of the Map Drive that give in PsDrive when created. The $((Get-Date).ToString('yyyy-MM-dd') will be create a folder with the date of the copy. Also use ErrorAction and ErrorVariable for error handling

To be able to do the comparison i use only the sum which is the number of the size in bytes in another variable

$var3=$var1.sum
$var4=$var2.sum

Remove the temporary mapdrive

Remove-PSDRive -name w

Do the comparison between the 2 folders and with the if statement decide what must be done. If the folders are equal then will be delete the source folder and Start the Service that must be started. Else just will write in log file file without do anything.

Askme4Tech is my Blog to the IT Community. From this site i share tips, news and in depth tutorials for IT Professionals working with Microsoft products.

My goal is to create a share Knowledge base for IT Professionals and Power Users that works with Microsoft Products and to provide valuable help in daily technical problems and keep up to date with news from IT industry.