3
votes

I have a folder on a net share - call it \Server\Backup\November.25.2013.backup. This folder has the sub folders \test1, \test2, \test3.

Example:

\\Server\Backup\November.25.2013.backup\
                                       .\Test1
                                       .\Test2
                                       .\Test3

I need to copy the sub folders of November.25.2013.backup to c:\Test. This function is only to copy the backup folder contents of a specified day (in this case yesterday's backup). I am using this script to restore the last day's backup minus the name (November.25.2013.backup). Here is what I have been trying to utilize:

Get-ChildItem -Path \\Server\Backup -r | Where-Object {$_.LastWriteTime -gt (Get-Date).Date}
% { Copy-Item -Path $_.FullName -Destination C:\Test -WhatIf }

However I get the error

    Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:3 char:20
+ % { Copy-Item -Path <<<<  $_.fullname -destination C:\Test -whatif }
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

Please understand that I am still a greenhorn with Powershell scripting and I am not sure how to troubleshoot this. I appreciate any advice.

My goal is to restore folders from a backup folder. Thanks.

2
You forgot to add a |(pipe) between the two statements. The %(foreach) has no input.Eris

2 Answers

3
votes

You are missing a pipe at the end of the first line.

Also, If you are trying to get a folder with a last write time of yesterday, it would be less than -lt the current date

Get-ChildItem -Path \\Server\Backup -r | Where-object {$_.lastwritetime -lt (get-date).date} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }

But that might grab more history than you want if there is a folder for every day. If you only wanted what was written yesterday use this:

Get-ChildItem -Path \\Server\Backup -r | Where-object {($_.lastwritetime.date -eq ((get-date).adddays(-1)).date)} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }

Example from comment:

Get-ChildItem -Path c:\test -r | Where-object {$_.PSIscontainer -and (($_.lastwritetime.date -eq ((get-date).adddays(-1)).date))}  |
% { Copy-Item $_.fullName -destination C:\Testoutput\ -recurse}
2
votes

(Adding as an answer as well.)

In your pasted code, there is no pipe between the Where-Object and %.

Simple solution: add a | to the end of the first line:

Get-ChildItem -Path \\Server\Backup -r | ? {$_.lastwritetime -gt (get-date).date} |
% { Copy-Item -Path $_.fullname -destination C:\Test -whatif }