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?

#1
Suppose I have the following snippet:

<!-- language: lang-powershell -->

$assoc = New-Object PSObject -Property @{
Id = 42
Name = "Slim Shady"
Owner = "Eminem"
}

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

I'd expect this snippet to show:

> `42 - Slim Shady - Eminem`

But instead it shows:

> `42 + - + Slim Shady + - + Eminem`

Which makes me think the `+` operator isn't appropriate for concatenating strings and variables.

How should you approach this with PowerShell?


Reply

#2
These answers all seem very complicated. If you are using this in a PowerShell script you can simply do this:

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

It will output

> My name is Slim Shady

Note how a space is put between the words for you
Reply

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

See the [Windows PowerShell Language Specification Version 3.0][1], p34, sub-expressions expansion.


[1]:

[To see links please register here]

Reply

#4
I just want to bring another way to do this using .NET [String.Format][1]:

$name = "Slim Shady"
Write-Host ([string]::Format("My name is {0}", $name))


[1]:

[To see links please register here]

Reply

#5
Concatenate strings just like in the DOS days. This is a big deal for logging so here you go:

$strDate = Get-Date
$strday = "$($strDate.Year)$($strDate.Month)$($strDate.Day)"

Write-Output "$($strDate.Year)$($strDate.Month)$($strDate.Day)"
Write-Output $strday
Reply

#6
I seem to struggle with this (and many other unintuitive things) every time I use PowerShell after time away from it, so I now opt for:

[string]::Concat("There are ", $count, " items in the list")

Reply

#7
[Write-Host][1] can concatenate like this too:

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

This is the simplest way, IMHO.

[1]:

[To see links please register here]


Reply

#8
You can also get access to C#/.NET methods, and the following also works:
```
$string1 = "Slim Shady, "
$string2 = "The real slim shady"

$concatString = [System.String]::Concat($string1, $string2)

Output:

Slim Shady, The real slim shady
```
Reply

#9
You need to place the expression in parentheses to stop them being treated as different parameters to the cmdlet:

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

Reply

#10
One way is:

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

Another one is:

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

Or just (but I don't like it ;) ):

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




Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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