none
Power Shell Script RRS feed

  • Question

  • Hi Everyone,

    I'm translating the following script from a foreign language website. I'm getting an error but I don't know how to fix it. 

    The script is suppose to get the files located in the txt and copy them over to the destination.

    Any help fixing this would be greatly appreciated !

    Here is the script: 

    $ List = Get-Content C:\Pstlist.txt
    $ Dest = "C:\Users\bob\Documents\Outlook Files"
    Foreach '($ line in $ list)' 
    {Xcopy $ line $ dest / y}

    I'm getting this error : 

    At line:3 char:8
    + Foreach '($ line in $ list)'
    +        ~
    Missing opening '(' after keyword 'foreach'.
    At line:3 char:8
    + Foreach '($ line in $ list)'
    +        ~
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword
    • Edited by SilentCow Monday, April 17, 2017 3:34 PM
    Monday, April 17, 2017 3:34 PM

Answers

  • Hi SilentCow,

    Kudos to you for trying to learn PowerShell!

    However - asking incremental questions in a forum is an extraordinarily inefficient way to learn anything. A forum is just not set up for the kind of interactive learning you need.

    We would recommend taking some classes in PowerShell. You will learn a lot, much more quickly, and avoid much frustration.

    This forum is designed to answer specific scripting questions. We're not equipped to teach people basic fundamentals, step-by-step. Trying to do this in a forum will frustrate both the teacher and the learner.


    -- Bill Stewart [Bill_Stewart]

    Monday, April 17, 2017 5:51 PM
    Moderator

All replies

  • There is no space after the dollar sign.  I recommend learning PowerShell be fore trying to modify scripts.  You are just making wild and very bad guesses.


    \_(ツ)_/

    Monday, April 17, 2017 3:38 PM
  • Yeah, I'm working on the learning part, jrv. Only on Chapter 7 of 28. 

    That being said, I do want to thank you for your quick response. I've eliminated the spaces and it now looks like this: 

    $List = Get-Content C:\Pstlist.txt
    $Dest = "C:\Users\bob\Documents\Outlook Files"
    foreach '($line in $list)' 
    {Xcopy $line $dest / y}

    but I still get this new error: 

    At line:3 char:8
    + foreach '($line in $list)'
    +        ~
    Missing opening '(' after keyword 'foreach'.
    At line:3 char:8
    + foreach '($line in $list)'
    +        ~
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

    Thank you all for your help. 

    Monday, April 17, 2017 3:47 PM
  • Make sure the foreach line looks the same as in your textbook.

    Guesswork and moving { } characters around will result in frustration, as jrv noted.


    -- Bill Stewart [Bill_Stewart]

    Monday, April 17, 2017 3:53 PM
    Moderator
  • Cool! Thanks Mr. Stewart.  I got a little further along. 

    I'm almost there. 

    My script now looks like this: 

    $List = Get-Content C:\Pstlist.txt
    $Dest = "C:\Users\bob\Documents\Outlook Files"
    foreach ($line in $list)
    {Xcopy $line $dest / y}

    but now I get this error: 

    xcopy.exe : Invalid switch - /At line:4 char:2
    + {Xcopy $line $dest / y}
    +  ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (Invalid switch - /:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    xcopy.exe : Invalid switch - /At line:4 char:2
    + {Xcopy $line $dest / y}
    +  ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (Invalid switch - /:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    xcopy.exe : Invalid switch - /At line:4 char:2
    + {Xcopy $line $dest / y}
    +  ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (Invalid switch - /:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    xcopy.exe : Invalid switch - /At line:4 char:2
    + {Xcopy $line $dest / y}
    +  ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (Invalid switch - /:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    I don't care how many tidbits of advice I get to make this thing work. I will become a Powershell master !

    Monday, April 17, 2017 4:10 PM
  •  help about foreach

    \_(ツ)_/

    Monday, April 17, 2017 4:30 PM
  • Hi SilentCow,

    Kudos to you for trying to learn PowerShell!

    However - asking incremental questions in a forum is an extraordinarily inefficient way to learn anything. A forum is just not set up for the kind of interactive learning you need.

    We would recommend taking some classes in PowerShell. You will learn a lot, much more quickly, and avoid much frustration.

    This forum is designed to answer specific scripting questions. We're not equipped to teach people basic fundamentals, step-by-step. Trying to do this in a forum will frustrate both the teacher and the learner.


    -- Bill Stewart [Bill_Stewart]

    Monday, April 17, 2017 5:51 PM
    Moderator