Skip to content
Fix Code Error

How do I concatenate strings and variables in PowerShell?

March 13, 2021 by Code Error
Posted By: Anonymous

Suppose I have the following snippet:

$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?

Solution

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

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

Answered By: Anonymous

Related Articles

  • What is the worst programming language you ever worked with?
  • Generated string names in Calculated Properties…
  • Reference — What does this symbol mean in PHP?
  • What does this symbol mean in JavaScript?
  • How to properly do JSON API GET requests and assign…
  • Simplest way to create Unix-like continuous pipeline…
  • Changing PowerShell's default output encoding to UTF-8
  • Get operating system info
  • Asynchronously running multiple PowerShell scripts from C#
  • How can I pass a wct test while rearranging children…
  • How can I find the product GUID of an installed MSI setup?
  • Reference - What does this regex mean?
  • Best practice multi language website
  • Error: the entity type requires a primary key
  • Azure Availability Zone ARM Config
  • Why does this Azure Resource Manager Template fail…
  • ComboBox.SelectedItem giving Null value
  • What are the undocumented features and limitations…
  • Binding complex object to a component
  • The 'compilation' argument must be an instance of…
  • How to parse JSON with XE2 dbxJSON
  • For-each over an array in JavaScript
  • Search match multiple values in single field in…
  • What is a NullReferenceException, and how do I fix it?
  • Testing API call in Vue with Moxios
  • no match for ‘operator
  • mongodb group values by multiple fields
  • Event Snippet for Google only shows one event while…
  • How can I get the current PowerShell executing file?
  • loop and eliminate unwanted lines with beautiful soup
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Smart way to truncate long strings
  • Maven2: Missing artifact but jars are in place
  • How to POST backbone model data to DB through Slim…
  • C++ template,typename and operator
  • What is the copy-and-swap idiom?
  • Spring data jpa- No bean named…
  • Change PowerShell object property names, retaining values
  • Overloading operators in typedef structs (c++)
  • Is there a way to run multiple powershell scripts in…
  • Azure CLI - az deployment group create -…
  • powershell - how to find and rename a specific field…
  • Why does the arrow (->) operator in C exist?
  • How can prevent a PowerShell window from closing so…
  • error NG6002: Appears in the NgModule.imports of…
  • What are the nuances of scope prototypal /…
  • From inside of a Docker container, how do I connect…
  • Avoid creating new session on each axios request laravel
  • NullpointerException error while working with…
  • How to implement an STL-style iterator and avoid…
  • How to change the color of vaadin-select-text-field…
  • Can Windows Containers be hosted on linux?
  • What is The Rule of Three?
  • Tomcat 7 "SEVERE: A child container failed during start"
  • What's the difference between Polymer's shady DOM vs…
  • Logging best practices
  • Backbone.js save gets '404 not found' on Slim.php
  • How to use Servlets and Ajax?
  • Octave using 'for' statement to show two animations…
  • Spring Boot - Cannot determine embedded database…
  • How to internationalize a React Native Expo App?
  • slim php get route with parameter (user login system)
  • Calculator: Back key doesnt work in Javascript
  • How do the PHP equality (== double equals) and…
  • Any reason not to use '+' to concatenate two strings?
  • Most effective way to parse JSON Objects
  • How to find the operating system version using JavaScript?
  • Slim 4 throws SlimExceptionHttpNotFoundException
  • How to connect to a docker container from outside…
  • What is the scope of variables in JavaScript?
  • LDAP root query syntax to search more than one specific OU
  • Use of Jquery on scroll event
  • Strange behaviour with powershell generic list
  • Read file line by line in PowerShell
  • Does moment.js allow me to derive a timezone…
  • Difference between variable declaration syntaxes in…
  • How do I limit the number of digits from 6 to 4 in…
  • Update the Azure DevOps service endpoint…
  • C# Get YouTube videoId from Json
  • Echo equivalent in PowerShell for script testing
  • Concatenate two NumPy arrays vertically
  • How to traverse the complex nested Json in C# and…
  • ErrorActionPreference behaving differently as…
  • Powershell - Set-Content : Cannot bind argument to…
  • Identifying and solving…
  • How to convert list of numpy arrays into single numpy array?
  • I am trying to subtract but it is always adding one…
  • How to concatenate two layers in keras?
  • Need constraints for y position or height when…
  • Error with huge list of AD groups. Not giving output
  • Python - Get command output cannot be decoded
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How do I concatenate two text files in PowerShell?
  • Angular 12 - Generating browser application bundles…
  • ./components/Avatar.tsx Error: Cannot find module…
  • How to switch languages with the i18next plugin?
  • Why is my locally-created script not allowed to run…
  • Ways to save Backbone.js model data?
  • PowerShell and the -contains operator
  • Centering in CSS Grid

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

“PKIX path building failed” and “unable to find valid certification path to requested target”

Next Post:

How can I cast int to enum?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error