I am really new to Powershell and scripting and would really appreciate if anyone is able to help.
I am trying to create a PowerShell script which does the following to backup any changes on a daily basis in a set number of directories.
Recursively find any new/modified files or directories from multiple preset locations only for the last 24 hours.
Compress all the resulting entries in a zip file
- while keeping the folder structures
I have managed to find get the first bit working using the following script:
Get-ChildItem $SRC | Where {$_.PsIsContainer} | foreach-object {
Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue |
Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} |
Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode
} >> C:\temp\LastModified-$TimeStamp.log
I have another script which zips up a given directory but I have been struggling to use the results from the script above to either robocopy the files to a location which I can then Zip or if there is a way to create a zip file without having to copy the files somewhere first without loosing the folder structure for each file?
I have the following script which can Zip a given directory (but I need to zip multiple directories into one zip file unless I robocopy the files somewhere first & how do I get robocopy to only copy only the files modified in the last 24 hours)
$Source = 'C:\Users\Downloads\RobocopyLocation\'
$Destination = 'D:\OneDrive\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($Source, $ZipName)
When I run the Script below I get and error message complaining about the path argument being null.
$F1 = 'C:\Users\Downloads\Folder A'
$F2 = 'C:\Users\Downloads\Folder B'
$F3 = 'C:\Users\Downloads\Folder C'
$SrcFolders = ($F1, $F2, $F3);
$BackupDrive = "D:\OneDrive"
$DestFolders = ("$BackupDrive\Folder A", "$BackupDrive\Folder B", "$BackupDrive\Folder C");
$Destination = 'C:\Users\SysAdmin\Downloads\PLSWork\'
$TimeStamp = Get-Date -f yyyyMMdd_HHmmss
$ZipName = "$Destination\$TimeStamp" + '_Backup.zip '
$Option = "{$_.PsIsContainer} | foreach-object { Get-ChildItem $_ -Recurse -ErrorAction SilentlyContinue | Where {$_.LastWriteTime -gt (get-date).AddDays(-1).date} | Select FullName, LastWriteTime, Mode | Sort LastWriteTime, Mode}"
Foreach ($SrcFolders in (Get-ChildItem $SrcFolders | where $Option)){
Add-Type -Assembly "System.IO.Compression.FileSystem" ;
[System.IO.Compression.ZipFile]::CreateFromDirectory($SrcFolders, $ZipName)
}
modify your line
with