• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How to handle spaces in command line paths?

X

xformer

#1
Hello,

I installed PowerShell to automate some tasks. I only write scripts and
stumled upon the following "feature": When a script is located in a directory
that has spaces in it I can't get PowerShell to execute the script. It always
stops parsing after the first space character.

Example: A script named "task.ps1" is located in "E:\All Batch Scripts\".

If I open a Windows explorer and double-click on the file. It doesn't work.
If I open a PowerShell and enter "E:\All Batch Scripts\task" it doesn't work
and shows the following message:

The expression "E:\All" was not recognized as a cmdlet, function, executable
program or script file. Check the expression and try again.
At Line:1 Character:7
+ E:\All <<<< Batch Scripts\task

If I put the script path into double or single quotes PowerShell just echoes
it.

I can get it to work by putting the path parts that contain spaces in single
quotes but that is not a general way to handle path names that contain spaces.

So my question is: What do I have to do so that PowerShell executes scripts
which have a path that contains spaces?

And the second question is how to put that in a link file in the autostart
directory so that the script is started whenever the user logs on?

Any help would be greatly appreciated.

Frank
 

My Computer

J

John Vottero

#2
Start with a & and enclose in quotes like this:

C:\> &"C:\My PowerShell Scripts\Check Disks.ps1"

"xformer" <xformer@discussions.microsoft.com> wrote in message
news:7E81A0C4-3C27-49D8-8590-CC7C55AF492A@microsoft.com...
> Hello,
>
> I installed PowerShell to automate some tasks. I only write scripts and
> stumled upon the following "feature": When a script is located in a
> directory
> that has spaces in it I can't get PowerShell to execute the script. It
> always
> stops parsing after the first space character.
>
> Example: A script named "task.ps1" is located in "E:\All Batch Scripts\".
>
> If I open a Windows explorer and double-click on the file. It doesn't
> work.
> If I open a PowerShell and enter "E:\All Batch Scripts\task" it doesn't
> work
> and shows the following message:
>
> The expression "E:\All" was not recognized as a cmdlet, function,
> executable
> program or script file. Check the expression and try again.
> At Line:1 Character:7
> + E:\All <<<< Batch Scripts\task
>
> If I put the script path into double or single quotes PowerShell just
> echoes
> it.
>
> I can get it to work by putting the path parts that contain spaces in
> single
> quotes but that is not a general way to handle path names that contain
> spaces.
>
> So my question is: What do I have to do so that PowerShell executes
> scripts
> which have a path that contains spaces?
>
> And the second question is how to put that in a link file in the autostart
> directory so that the script is started whenever the user logs on?
>
> Any help would be greatly appreciated.
>
> Frank
 

My Computer

R

RichS

#3
To run a script in that manner try

&"c:\test three\test3.ps1"

& tells Powershell to execute the string rather than just echoing it
back. if you need parameters etc put them oustide the quotes

your command is

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command
&"c:\test three\test3.ps1"


--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"xformer" wrote:

> Hello,
>
> I installed PowerShell to automate some tasks. I only write scripts and
> stumled upon the following "feature": When a script is located in a directory
> that has spaces in it I can't get PowerShell to execute the script. It always
> stops parsing after the first space character.
>
> Example: A script named "task.ps1" is located in "E:\All Batch Scripts\".
>
> If I open a Windows explorer and double-click on the file. It doesn't work.
> If I open a PowerShell and enter "E:\All Batch Scripts\task" it doesn't work
> and shows the following message:
>
> The expression "E:\All" was not recognized as a cmdlet, function, executable
> program or script file. Check the expression and try again.
> At Line:1 Character:7
> + E:\All <<<< Batch Scripts\task
>
> If I put the script path into double or single quotes PowerShell just echoes
> it.
>
> I can get it to work by putting the path parts that contain spaces in single
> quotes but that is not a general way to handle path names that contain spaces.
>
> So my question is: What do I have to do so that PowerShell executes scripts
> which have a path that contains spaces?
>
> And the second question is how to put that in a link file in the autostart
> directory so that the script is started whenever the user logs on?
>
> Any help would be greatly appreciated.
>
> Frank
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)