4

This is my powershell script test.ps1:

Write-Output $args;

Now suppose I have a batch script that calls this powershell script with all kinds of paths. One of those is c:\:

powershell -executionpolicy Bypass -file test.ps1 "c:\"

The output is:

c:"

Is there any way to quote my arguments such that c:\ would actually be taken and stored as is in the $args[0] variable? I know I can solve this quick'dirty by passing "c:\\", but that's not a real solution.

EDIT: using named parameters in test.ps1 doesn't make any difference:

[CmdletBinding()]
param(
    [string]$argument
)
Write-Output $argument;

EDIT2: using a batch file instead works fine.

My test.bat script:

echo %~1

I run it:

test.bat "c:\"

Returns nicely:

c:\

1
  • The backslash is being used as an escape character it seems but only if it is the last character in the quoted string. Commented Jun 22, 2015 at 20:06

3 Answers 3

1

Are you sure this comes form powershell and not from the program which invokes your statement? The backslash is no escape code in powershell.

my test.ps1 is working, when run from ise.

this works for me:

powershell -executionpolicy Bypass -command "test.ps1 -argument 'C:\'"

(end with quote double-quote)

Sign up to request clarification or add additional context in comments.

7 Comments

I am executing it from cmd. So cmd is the culprit? If I write a simple test.bat script with code echo %*, I get c:\ back. So it doesn't seem to be cmd causing problem.
maybe, have you tried 'C:\' or even c:\ without quotes?
'c:\' gives back 'c:\' and c:\ gives c:\ but that last one is not an option because possible spaces in the path.
@davor I am able to reproduce this "issue" in a batch. I am not aware of the solution just yet. Pehaps cmd is escaping the "
This should have been a comment, not an answer. With a bit more rep, you will be able to post comments. Until then, please do not use answers as a workaround.
|
1

Help file for PowerShell.exe says:

File must be the last parameter in the command, because 'all characters' typed after the file parameter name are "interpreted" as the script file path followed by the script parameters.

You are against Powershell.exe's command line parser, which uses "\" to escape quotes. Do you need quotes? Not in your case:

powershell -file test.ps1 c:\

prints

c:\

Similarly, this works too

powershell -file test.ps1 "c:\ "
c:\

but then your arg has that extra space which you would want to trim. BTW, Single quotes do not help here:

powershell -file test.ps1 'c:\'
'c:\' 

2 Comments

I need quotes because file paths might contain spaces. One of the reasons to use -File is because 'all characters' typed after the file parameter name are "interpreted" as the script file path followed by the script parameters. But I don't understand how that quote explains the escaping of the last quote... When I read the quote, I don't expect any escaping, and certainly not of the last (?) character.
@davor It's the powershell.exe command line syntax parsing that causes it. If you needed to pass quotes, you would have used \" to do that, which is what it thinks is happening
0

If you need the final backlash to be passed to the command, you can use

$ArgWithABackslashTemp = $ArgWithABackslash -replace '\\$','\\'
&$ExePath $ArgWithABackslashTemp

Or, if the exe is smart enough to handle it without the trailing backslash

&$ExePath $ArgWithABackslash.trim('\')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.