3

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.

1
  • You forgot to add a |(pipe) between the two statements. The %(foreach) has no input. Commented Nov 26, 2013 at 20:15

2 Answers 2

3

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}
Sign up to request clarification or add additional context in comments.

4 Comments

This works except for now the folders come across but no files are in them do I need to add this !($_.psiscontainer) -AND to my script
Yes that will work, just be sure to add -Recurse at the end of the copy-item where -whatif is currently. I can add it as a separate example. Please mark this question as answered if you feel it is and start a new thread for any other issues.
Works like a charm - Many thanks Please consider this Question Answered
When someone answers your question, rather than posting a comment thanking the answerer, you should mark the answer as accepted. You can also upvote any answers that you found useful once you have at least 15 rep (including the accepted answer). Note that Eris pointed out the missing pipe first, but this answer addresses additional issues. Please decide which answer you feel best resolved your issue and mark it as accepted. See here for more info: stackoverflow.com/help/someone-answers
2

(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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.