如何在 PowerShell 中连接字符串和变量? [英] How do I concatenate strings and variables in PowerShell?

查看:79
本文介绍了如何在 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

但它显示:

42 + - + Slim Shady + - + Eminem

这让我觉得 + 运算符不适合连接字符串和变量.

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

您应该如何使用 PowerShell 解决这个问题?

How should you approach this with PowerShell?

推荐答案

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

请参阅 Windows PowerShell 语言规范版本 3.0,p34,子表达式扩展.

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

这篇关于如何在 PowerShell 中连接字符串和变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆