Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Passing parameters to PS script and escaping spaces

Reply
 
Old 08-14-2007   #1 (permalink)
James
Guest


 
 

Passing parameters to PS script and escaping spaces

I have written a PS script which requires two parameters to be passed in: one
folder name and one number. If I open a PS console I can execute the script
like this:

c:\myscript.ps1 "c:\my folder\" 1

I am now trying to run this script from a standard cmd console like this:

powershell -command c:\myscript.ps1 "c:\my folder\" 1

However the quotes don't seem to work and the script thinks it is getting 3
parameters passed.

How do I launch a script and pass parameters which may include spaces? Out
of interest, how would I launch it if the path to the script also contains a
space, eg:

"c:\my folder\myscript.ps1" "c:\my folder\" 1

My System SpecsSystem Spec
Old 08-14-2007   #2 (permalink)
James
Guest


 
 

RE: Passing parameters to PS script and escaping spaces

After playing around a bit I found this seems to work:

powershell -command "& 'c:\my script.ps1' 'c:\my folder\' 1"

Are there any other "better" ways?


"James" wrote:

> I have written a PS script which requires two parameters to be passed in: one
> folder name and one number. If I open a PS console I can execute the script
> like this:
>
> c:\myscript.ps1 "c:\my folder\" 1
>
> I am now trying to run this script from a standard cmd console like this:
>
> powershell -command c:\myscript.ps1 "c:\my folder\" 1
>
> However the quotes don't seem to work and the script thinks it is getting 3
> parameters passed.
>
> How do I launch a script and pass parameters which may include spaces? Out
> of interest, how would I launch it if the path to the script also contains a
> space, eg:
>
> "c:\my folder\myscript.ps1" "c:\my folder\" 1

My System SpecsSystem Spec
Old 08-14-2007   #3 (permalink)
Brandon Shell
Guest


 
 

Re: Passing parameters to PS script and escaping spaces

Try this
powershell -noprofile -command c:\tools\scripts\test.ps1 {"c:\my folder\"} 1

"James" <James@discussions.microsoft.com> wrote in message
news:C2CB354F-BEC8-460C-9229-DA360642E5CF@microsoft.com...
>I have written a PS script which requires two parameters to be passed in:
>one
> folder name and one number. If I open a PS console I can execute the
> script
> like this:
>
> c:\myscript.ps1 "c:\my folder\" 1
>
> I am now trying to run this script from a standard cmd console like this:
>
> powershell -command c:\myscript.ps1 "c:\my folder\" 1
>
> However the quotes don't seem to work and the script thinks it is getting
> 3
> parameters passed.
>
> How do I launch a script and pass parameters which may include spaces?
> Out
> of interest, how would I launch it if the path to the script also contains
> a
> space, eg:
>
> "c:\my folder\myscript.ps1" "c:\my folder\" 1


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Passing parameters to a cmdlet in a variable PowerShell
Hosting PowerShell & passing parameters to script PowerShell
passing [switch] parameters PowerShell
Passing parameters to a PS script PowerShell
passing parameters to script (ps1) PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46