Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 510 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Powershell is removing comma from program argument

#1
I want to pass in a comma separated list of values into a script as part of a single switch.

Here is the program.

param(
[string]$foo
)

Write-Host "[$foo]"

Here is the usage example

PS> .\inputTest.ps1 -foo one,two,three

I would expect the output of this program to be `[one,two,three]` but instead it is returning `[one two three]`. This is a problem because I want to use the commas to deliminate the values.

Why is powershell removing the commas, and what do I need to change in order to preserve them?
Reply

#2
The comma is a special symbol in powershell to denote an array separator.

Just put the arguments in quotes:

inputTest.ps1 -foo "one, two, three"

Alternatively you can 'quote' the comma:

inputTest.ps1 -foo one`,two`,three

Reply

#3
Enclose it in quotes so it interprets it as one variable, not a comma-separated list of three:

>PS> .\inputTest.ps1 -foo "one,two,three"
Reply

#4
Following choices are available

1. Quotes around all arguments (')

<code>inputTest.ps1 -foo 'one,two,three'</code>

2. Powershell's escape character before each comma (grave-accent (`))

<code>inputTest.ps1 -foo one`,two`,three</code>

**Double quotes** around arguments don't change anything !
**Single quotes** eliminate expanding.
**Escape character** (grave-accent) eliminates expanding of next character
Reply

#5
if you are passing parameters from a batch file, then enclse your params as below
"""Param1,Param2,Param3,Param4"""
Reply

#6
Powershell will remove the commas and use them to delimitate an array. You can join the array elements back into a string and put a comma between each one.

param(
[string[]]$foo
)
$foo_joined = $foo -join ","
write-host "[$foo_joined]"

Any spaces between your arguments will be removed, so this:

`PS> .\inputTest.ps1 -foo one, two, three`

will also output: `[one,two,three]`

If you need the spaces you'll need quotes, so do this:

`PS> .\inputTest.ps1 -foo one,"two is a pair"," three"`

if you want to output: `[one,two is a pair, three]`
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through