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:
  • 615 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I concatenate strings and variables in PowerShell?

#11
Try wrapping whatever you want to print out in parentheses:

Write-Host ($assoc.Id + " - " + $assoc.Name + " - " + $assoc.Owner)

Your code is being interpreted as many parameters being passed to [`Write-Host`][1]. Wrapping it up inside parentheses will concatenate the values and then pass the resulting value as a single parameter.

[1]:

[To see links please register here]



Reply

#12
Here is another way as an alternative:

Write-Host (" {0} - {1} - {2}" -f $assoc.Id, $assoc.Name, $assoc.Owner)
Reply

#13
Another option is:

$string = $assoc.ID
$string += " - "
$string += $assoc.Name
$string += " - "
$string += $assoc.Owner
Write-Host $string

The "best" method is probably the one C.B. suggested:

Write-Host "$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)"





Reply

#14
(Current PowerShell version 5.1.17134.407)

This also works as of now:

$myVar = "Hello"

echo "${myVar} World"

Note: this only works with double quotes

Reply

#15
Just for the fun. You can also access the values of the PSObject directly like below:

$assoc.psobject.Properties.value -join " - "

But if you do not specify that the object should be ordered, PowerShell will display the values in a random order. So you should add the flag `[ordered]`:

$assoc = [pscustomobject] [ordered] @{
Id = 42
Name = "Slim Shady"
Owner = "Eminem"
}



Reply

#16
If you're concatenating strings to build file paths, use the [Join-Path][1] command:

Join-Path C:\temp "MyNewFolder"

It'll automatically add the appropriate trailing / leading slashes for you, which makes things a lot easier.

[1]:

[To see links please register here]

Reply

#17
There is a difference between single and double quotes. (I am using PowerShell 4).

You can do this (as [Benjamin said][1]):

$name = 'Slim Shady'
Write-Host 'My name is'$name
-> My name is Slim Shady

Or you can do this:

$name = 'Slim Shady'
Write-Host "My name is $name"
-> My name is Slim Shady

The single quotes are for literal, output the string exactly like this, please.
The double quotes are for when you want some pre-processing done (such as variables, special characters, etc.)

So:

$name = "Marshall Bruce Mathers III"
Write-Host "$name"
-> Marshall Bruce Mathers III

Whereas:

$name = "Marshall Bruce Mathers III"
Write-Host '$name'
-> $name

(I find *[How-to: Escape characters, Delimiters and Quotes][2]* good for reference).


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#18
While expression:

"string1" + "string2" + "string3"

will concatenate the string, you need to put a $ in front of the parenthesis to make it evaluate as a single argument when passed to a PowerShell command. Example:

Write-Host $( "string1" + "string2" + "string3" )

As a bonus, if you want it to span multiple lines, then you need to use the awkward backtick syntax at the end of the line (without any spaces or characters to the right of the backtick).

Example:

Write-Host $(`
"The rain in " +`
"Spain falls mainly " +`
"in the plains" )`
-ForegroundColor Yellow

(Actually, I think PowerShell is currently implemented a little bit wrong by requiring unnecessary backticks between parentheses. If Microsoft would just follow [Python][1] or [Tcl][2] parenthesis rules of allowing you to put as many newlines as you want between the starting and ending parenthesis then they would solve most of the problems that people don't like about PowerShell related to line continuation, and concatenation of strings.

I've found that you can leave the backticks off sometimes on line continuations between parenthesis, but it's really flaky and unpredictable if it will work... It's better to just add the backticks.)


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#19
$assoc = @{
Id = 34
FirstName = "John"
LastName = "Doe"
Owner = "Wife"
}

$assocId = $assoc.Id
$assocFN = $assoc.FirstName
$assocLN = $assoc.LastName
$assocName = $assocFN, $assocLN -Join " "
$assocOwner = $assoc.Owner

$assocJoin = $assocId, $assocName, $assocOwner -join " - "
$assocJoin
#Output = 34 - John Doe - Wife
Reply

#20
As noted elsewhere, you can use join.

If you are using commands as inputs (as I was), use the following syntax:

```
-join($(Command1), "," , $(Command2))
```

This would result in the two outputs separated by a comma.

See

[To see links please register here]

for related comment
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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