locked
powershell: copying files into a backupfolder RRS feed

  • Question

  • Hi All,

    I am creating a script to copy items that I created today in a backupfolder called back. Below is the script but when I run it, gives me the

    error: "week5.ps1 is not a name of a commandlet"

    backup folder name :  back

    scipt name: week5.ps1

    script:

                       gci | where-object {$_.lastwritetime.date -eq (get-date).date} |

                       foreach-object {$_ copy-item  c:\cmd.practise\back}

    Thank-you

    SQL 75

    Monday, April 6, 2015 2:29 AM

Answers

  • So the original error was not the issue at all.

    What is this supposed to do? It will never find anything:
    $_.lastwritetime.date -eq (get-date).date

    Can you see why?

    gci | 
         where-object{ $_.lastwritetime.date -eq (get-date).date} |
             foreach-object{
                   $_ | copy-item  c:\cmd.practise\back
             }
    

    You were missing a pipe.  It also appears that you are trying too copy everything over the top of itself which will not work.

    You need to think about what you are doing. Ask yourself  if the code makes sense.


    \_(ツ)_/


    • Edited by jrv Monday, April 6, 2015 7:59 AM
    • Marked as answer by SQL75 Monday, April 6, 2015 5:58 PM
    Monday, April 6, 2015 7:57 AM

All replies

  • You need to post the whole error message and not just a piece of it.

    \_(ツ)_/

    Monday, April 6, 2015 3:39 AM
  • How are you calling the posh script?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Milton Goh
    MCSE, MCSA, MCITP, MOF, ITIL, AIIM SharePoint Specialist

    Blog | Twitter

    Monday, April 6, 2015 3:49 AM
  • actually I am calling it by using .\back\week5.ps1

    It copies the files and also spits out the errors in red on the POSH screen as below:


    copy-item : An item with the specified name C:\cmd.practise\back\backup already exists.
    At C:\cmd.practise\back\week5.ps1:5 char:17
    + foreach-object {copy-item $_ back}
    +                 ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceExists: (C:\cmd.practise\back\backup:String) [Copy-Item], IOException
        + FullyQualifiedErrorId : DirectoryExist,Microsoft.PowerShell.Commands.CopyItemCommand

    copy-item : Cannot overwrite the item C:\cmd.practise\back with itself.
    At C:\cmd.practise\back\week5.ps1:5 char:17
    + foreach-object {copy-item $_ back}
    +                 ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\cmd.practise\back:String) [Copy-Item], IOException
        + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

    Thanks

    SQL 75



    • Edited by SQL75 Monday, April 6, 2015 5:20 AM
    Monday, April 6, 2015 5:19 AM
  • So the original error was not the issue at all.

    What is this supposed to do? It will never find anything:
    $_.lastwritetime.date -eq (get-date).date

    Can you see why?

    gci | 
         where-object{ $_.lastwritetime.date -eq (get-date).date} |
             foreach-object{
                   $_ | copy-item  c:\cmd.practise\back
             }
    

    You were missing a pipe.  It also appears that you are trying too copy everything over the top of itself which will not work.

    You need to think about what you are doing. Ask yourself  if the code makes sense.


    \_(ツ)_/


    • Edited by jrv Monday, April 6, 2015 7:59 AM
    • Marked as answer by SQL75 Monday, April 6, 2015 5:58 PM
    Monday, April 6, 2015 7:57 AM