Windows Vista Forums

Call ps1 with multiple args that contain spaces
  1. #1


    cgkenney Guest

    Call ps1 with multiple args that contain spaces

    I've tried just about every combination to execute the script in a bat
    file to no avail. Any help is appreciated.

    Script accepts 6 arguments:

    C:>powershell.exe -noexit -noprofile & 'C:\PowerShell Scripts\Program
    Controller v0.2.ps1' 'C:\Folder Name\Staging\' 'C:\Folder Name\Staging
    \Inbound\' .txt move 100 N

    Powershell console is invoked but no work is done. In some cases,
    using double-quotes, I get the "Ampersand not allowed" msg. I've
    tried wrapping the command in curly braces as well as using the -
    command parameter.

    The following works in the PowerShell console:

    PS C:\PowerShell Scripts> .\"Program Controller v0.2.ps1" "C:\Folder
    Name\Staging\" "C:\Folder Name\Staging\Inbound\" .txt move 100 N



    Any ideas?

      My System SpecsSystem Spec

  2. #2


    Shay Levy [MVP] Guest

    Re: Call ps1 with multiple args that contain spaces

    Hello cgkenney@xxxxxx,

    Put this lines at the top of the script, it will help you detemine what args
    are exposed to the script:

    for($i=0;$i -lt $args.length;$i++){
    "Arg $i is <" +$args[$i]+">"
    }



    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar


    > I've tried just about every combination to execute the script in a bat
    > file to no avail. Any help is appreciated.
    >
    > Script accepts 6 arguments:
    >
    > C:>powershell.exe -noexit -noprofile & 'C:\PowerShell Scripts\Program
    > Controller v0.2.ps1' 'C:\Folder Name\Staging\' 'C:\Folder Name\Staging
    > \Inbound\' .txt move 100 N
    >
    > Powershell console is invoked but no work is done. In some cases,
    > using double-quotes, I get the "Ampersand not allowed" msg. I've
    > tried wrapping the command in curly braces as well as using the -
    > command parameter.
    >
    > The following works in the PowerShell console:
    >
    > PS C:\PowerShell Scripts> .\"Program Controller v0.2.ps1" "C:\Folder
    > Name\Staging\" "C:\Folder Name\Staging\Inbound\" .txt move 100 N
    >
    > Any ideas?
    >


      My System SpecsSystem Spec

  3. #3


    cgkenney Guest

    Re: Call ps1 with multiple args that contain spaces

    Thanks for your reply.

    It doesn't appear that the script is executing at all: no output is
    displayed and the actions (e.g. Copy-Item) are not performed. The
    result is simply that the Powershell console is enabled within the
    cmd.exe shell. In some cases, depending on how the arguments are
    wrapped with quotations in the call, the script file itself is loaded
    into memory - in edit mode within notepad.

    Dumping the args array to stdout when running the script within the
    Powershell ISE shows all of the args are getting through ok. An
    alternative would be to create and use an ini-type file to store the
    arguments but using commandline parameters scales better.

      My System SpecsSystem Spec

  4. #4


    Shay Levy [MVP] Guest

    Re: Call ps1 with multiple args that contain spaces


    I created a script file in this path 'D:\Scripts\temp\foo bar\script.ps1'
    with a conetnt of:

    for($i=0;$i -lt $args.length;$i++){
    "Arg $i is <" +$args[$i]+">"
    }


    Then I executed this command in start>run box:

    powershell.exe -noexit -noprofile & 'D:\Scripts\temp\foo bar\script.ps1'
    'C:\Folder Name\Staging\' 'C:\Folder Name\Staging \Inbound\' .txt move 100 N


    The output shows that all args are in place, do you get similar results?

    Arg 0 is <C:\Folder Name\Staging\>
    Arg 1 is <C:\Folder Name\Staging \Inbound\>
    Arg 2 is <.txt>
    Arg 3 is <move>
    Arg 4 is <100>
    Arg 5 is <N>




    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    > Thanks for your reply.
    >
    > It doesn't appear that the script is executing at all: no output is
    > displayed and the actions (e.g. Copy-Item) are not performed. The
    > result is simply that the Powershell console is enabled within the
    > cmd.exe shell. In some cases, depending on how the arguments are
    > wrapped with quotations in the call, the script file itself is loaded
    > into memory - in edit mode within notepad.
    >
    > Dumping the args array to stdout when running the script within the
    > Powershell ISE shows all of the args are getting through ok. An
    > alternative would be to create and use an ini-type file to store the
    > arguments but using commandline parameters scales better.
    >


      My System SpecsSystem Spec

Call ps1 with multiple args that contain spaces problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Call function with parameters that also call functions (.Net and P bsdz PowerShell 2 10 Jun 2008
multiple desktops in Vista? (similar to spaces in osx leopard) evero General Discussion 3 23 Apr 2008
Is this a bug with @args RickB PowerShell 4 09 Apr 2008
$args IT Staff PowerShell 7 19 Jan 2008
Info: If an object has multiple scriptmethods. Can one Scriptmethod call another? Brandon Shell PowerShell 3 19 Sep 2006