1

\I have tried everything I can find on the web but I just can't make this work.... Really frustrating. All I want to do is to pass in a file name in the command line to my Powershell script test.ps1.

param([String]$input=$args[0])
$inputpath = 'C:\work\'
$inputfile = $($inputpath + $input+".txt")

I run the script like:

powershell .\test.ps1 "input"

However, the in the error message I print out, I keep getting "cannot find c:\work\.txt". Apparently my command line parameter isn't in there correctly. Can someone please help?

4
  • shouldn't the error be can't find c:\work\.txt? if $input isn't defined in there, powershell wouldn't magically strip off the trailing backslash in $inputpath. Commented Jul 10, 2013 at 16:22
  • You're right. just edited. I didn't know I needed to put "\\" for the "\" to show up in stackoverflow. Commented Jul 10, 2013 at 16:25
  • So what does $args contain? Dump it, I'd say... Commented Jul 10, 2013 at 16:25
  • It works when I hard code the file name in the script. But I want to learn how to pass in the file name in command line. Commented Jul 10, 2013 at 16:28

2 Answers 2

4

Try to change $Input into some other name. Because $Input is used for Pipeline variable.

See for example:

http://blogs.technet.com/b/heyscriptingguy/archive/2010/12/31/write-powershell-functions-that-accept-pipelined-input.aspx

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

Comments

0

I'd simply use this for building the path:

$inputpath = 'C:\work\'
$inputfile = Join-Path $inputpath ($args[0] + ".txt")

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.