Solved

powershell joinging strings without a space

Posted on 2014-11-16
13
179 Views
Last Modified: 2014-11-16
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
}
0
Comment
Question by:Dead_Eyes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40446042
use the following instead: ( 2 single quotes with no space in between)

write-host $_.Name,$Ending -Separator ''
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 500 total points
ID: 40446059
Easier still is to use write-host "$($_,name)$ending"
0
 

Author Comment

by:Dead_Eyes
ID: 40446083
Hi, thanks for the quick response unfortunately both suggestions did not work and just errored out :(
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Neil Russell
ID: 40446085
Sorry, a TYPO in mine.

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

. not ,
0
 

Author Comment

by:Dead_Eyes
ID: 40446088
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
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40446098
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?
0
 

Author Comment

by:Dead_Eyes
ID: 40446101
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"
}
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40446112
The first would not no but the second yes.
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40446116
Once you understand the concept of using "$($var.prop)" you can build complex strings quick and easily.
0
 

Author Comment

by:Dead_Eyes
ID: 40446120
Thanks for the great explanation and the extra help Neilsr. I am getting it now
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40446127
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
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40446162
Oops zero points even I posted the correct answer and the first one to post
0
 

Author Comment

by:Dead_Eyes
ID: 40446167
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
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Power Action 2 38
Window Passwordy Expiration and Powershell 5 38
Get all Files under specific folder 1 30
PowerShell script to get http response code 3 67
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question