Avatar of Dead_Eyes
Dead_Eyes
Flag for United Kingdom of Great Britain and Northern Ireland asked on

powershell joinging strings without a space

Hi, I am still learning PowerShell and attempting to resolve a problem I found when exploring the functionality of the ForEach-Object Cmdlet. When I attempt to join the 2 variables a space appears in between them and I can't seem to get rid of it no matter what I try. I think its something to do with the Output field separator but after trying several different tactics to change it I am at a loss. I have noticed if I use a txt file instead of a .csv & Get-Content the problem does not occur but using .txt files would limit the use's I am envisioning for ForEach-Object. Can anyone offer a solution preferably with an explanation of why and how to manipulate variables in the loop? thanks in advance

$var1 = Import-Csv -Path D:\Book1.csv
$Ending = "@Domain.com"
$Var1 | ForEach-Object {
Write-Host $_.Name,$Ending
}
Powershell

Avatar of undefined
Last Comment
Dead_Eyes

8/22/2022 - Mon
Raheman M. Abdul

use the following instead: ( 2 single quotes with no space in between)

write-host $_.Name,$Ending -Separator ''
ASKER CERTIFIED SOLUTION
Neil Russell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Dead_Eyes

ASKER
Hi, thanks for the quick response unfortunately both suggestions did not work and just errored out :(
Neil Russell

Sorry, a TYPO in mine.

write-host "$($_.name)$ending"

. not ,
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Dead_Eyes

ASKER
Sorry take that back I just made a school boy syntax error. Thanks for the response's. Neilsr I like your phrasing but  Raheman Mohammed Abdul's answer looks far more easy to understand when learning. Could you point me to the how and why of your syntax? thanks
Neil Russell

OK, using $variables inside a string is normally just a case of doing "$variablename" and the contents of the variable will be used in the string.

However when you are using an object property, i.e.  $_.name ,  where $_ is an object and .name is a property of the object, you need to use the "$($variable.property)" method.  the $() around the variable and property just instruct powershell to treat the contents between the $(  and the  )  as a whole variable expression.

Without the $() around it, "$variable.property"  would be evaluated as $variable followed by the litteral text of ".property"

Hope thats clear?
Dead_Eyes

ASKER
Sorry and finally would this work in a loop like this:
$var1 = Import-Csv -Path D:\Book1.csv
$Ending = "@Domain.com"
$Var1 | ForEach-Object {
New-ADUser -Name $_.Name -UserPrincipalName $_.Name,$Ending -Separator ''
}
# Or using the 2nd example
$var1 = Import-Csv -Path D:\Book1.csv
$Ending = "@Domain.com"
$Var1 | ForEach-Object {
New-ADUser -Name $_.Name -UserPrincipalName "$($_.name)$ending"
}
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Neil Russell

The first would not no but the second yes.
Neil Russell

Once you understand the concept of using "$($var.prop)" you can build complex strings quick and easily.
Dead_Eyes

ASKER
Thanks for the great explanation and the extra help Neilsr. I am getting it now
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Neil Russell

You could have an object called $User with properties of firstname, lastname and Domain for example and the email address would be.....

"$($User.firstname).$($User.lastname)@$($User.Domain)"

The . and the @ between the $($) pairs are literal chars so will get, for example.

Fred.Blogs@SomeDomain.com
Raheman M. Abdul

Oops zero points even I posted the correct answer and the first one to post
Dead_Eyes

ASKER
Sorry Raheman I know your answer was correct but Neilsr really went the extra mile with a great explanation and his way is far more flexible
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.