I am new to powershell scripting and i cant figure out why my script copies all files and doesn't seem to check the date and then copies all the files anyway. I was trying to do by days and minutes too, but I am not quite sure on how to do that. any help would be great!
see my script below.
$RemotePath = "\\eb-pc\E$\testlocation\*.txt"
$LocalPath = "C:\testlocation"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date
#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
{
Copy-Item -Path $file.fullname -Destination $LocalPath
#Move-Item -Path $file.fullname -Destination $LocalPath
}
}
Use PowerShell to gather your files based on date/time, and then use robocopy to copy them. $cutoff = Get-Date '1/16/2015 18:00' Get-ChildItem .\ -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach { $file = $_.Fullname robocopy ...
Below is the PowerShell script to find files modified in the last 3 days. Get-ChildItem -Path "E:\Folder1" | Where-Object { $_.CreationTime -gt (Get-Date).AddDays (-3) } | Select-Object Fullname Now we will see how we find the last modified time of files inside a folder using PowerShell.
The PowerShell copy-item cmdlet means that you can copy an item from one location to another location in the same namespace. When you run the PowerShell copy item cmdlet, it will overwrite the file by default if it already exists. It can copy a file to a folder, but it can’t copy a file to a certificate drive.
The particular items that the cmdlet can copy rely on the PowerShell provider that exposes the items. For example, it can copy files and directories in a file system drive and entries and registry keys in the registry drive. In addition, the Powershell copy files cmdlet can copy and rename the files in the same command.
If you want to use Hours and minutes, instead of AddDays, just use the .AddMinutes(), .AddHours(), or .AddSeconds() methods instead.
For what it's worth, I made a small modifcation, adding an Else{Scriptblock} to the script to echo out the files which aren't being copied. As written your code will only copy files written in the last 24 hours.
$RemotePath = "t:\"
$LocalPath = "C:\temp"
$Max_days = "-1"
#Max_mins = "-5"
$Curr_date = get-date
#Checking date and then copying file from RemotePath to LocalPath
Foreach($file in (Get-ChildItem $RemotePath))
{
if($file.LastWriteTime -gt ($Curr_date).adddays($Max_days))
{
Copy-Item -Path $file.fullname -Destination $LocalPath -WhatIf
#Move-Item -Path $file.fullname -Destination $LocalPath
}
ELSE
{"not copying $file"
}
}
>What if: Performing the operation "Copy File" on target "Item: T:\file.htm Destination: C:\temp\file.ht
m".
not copying ListOfSacredVMs.txt
not copying newUser_01.png
not copying newUser_015.png
not copying newUser_02.png
not copying newUser_03.png
What if: Performing the operation "Copy File" on target "Item: T:\values.csv Destination: C:\temp\values.csv".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With