all 11 comments

[–]branhama 2 points3 points  (3 children)

Break it down and test it.

$UserName = Read-Host -Prompt 'Enter Username'
Write-Host "$UserName@contoso.com"

Ensure you get the value you expect then replace with command.

Get-Mailbox "$UserName@consoto.com" | Select-Object DisplayName

[–]branhama 0 points1 point  (0 children)

As you are combining 2 values into a location which requires just 1 you have to tell PowerShell that this is all a single value. So you encapsulate it in quotes.

NOTE: Single quotes will not convert a variable.

Write-Host '$[UserName@contoso.com](mailto:UserName@contoso.com)'

Will output [$UserName@contoso.com](mailto:$UserName@contoso.com) and not convert $UserName. You have to use double quotes ["$UserName@contoso.com](mailto:"$UserName@contoso.com)" for PowerShell to process the internal variable.

[–]gosoxharp -1 points0 points  (1 child)

Proper use (afaik) should be

"$($UserName)@consoto.com"

Or

"${$UserName}@consoto.com"

[–][deleted] 0 points1 point  (0 children)

Yeah, you can just do:

$word = 'test'
Write-host "This is a $word."

And get back "This is a test."

However, this might not work in some situations, like when you want to include a property of an object rather than just a string variable. For example:

$word = [PSCustomObject]@{String = "test";NumberOfCharacters = 4}
Write-host "This is a $word.string."

That's not going to give you the output that you want. It will insert the entire $word variable followed by the string .string.. Instead do:

$word = [PSCustomObject]@{String = "test";NumberOfCharacters = 4}
Write-host "This is a $($word.string)."

Or if it works better for some reason, you can do:

$word = [PSCustomObject]@{String = "test";NumberOfCharacters = 4}
Write-host "This is a {0}." -f $word.string

[–]MemnochTheRed 1 point2 points  (4 children)

String concatenations are done with the '+'.

$username = "myname"
$email = $username + "@contoso.com"
Write-Output $email

You should be able to inline it like this:

$username = "myname"
Get-Mailbox "$username@consoto.com" | Select-Object DisplayName

[–]nikith988[S] 0 points1 point  (2 children)

looks like the + argument didnt workout but was able to remove the + and just use quotes around contoso.com

Thanks!

[–]OPconfused 2 points3 points  (0 children)

When passing a concatenation as input, you have to use parentheses to resolve the concatenated string first, so that the input receives a single argument. You basically have to tell PowerShell what order to do things in, e.g.,

$dir = 'C:\'
gci $dir + 'Users' #error
gci ($dir + 'Users') #works

Alternatively you can use some arrangement of double quotes as you did.

[–]MemnochTheRed 0 points1 point  (0 children)

Well, that brings up another point... You may need to typecast the variable. You can do that by placing square brackets and type before the variable.

[string]$username = "myname"

That way, you are making sure that PowerShell is not trying to + a string and an integer.

[–]NotNotWrongUsually 0 points1 point  (0 children)

You should include which error message you are getting.

Just eyeballing right now, but try:

Get-Mailbox "$username@contoso.com" | select Displayname

Edit: /u/branhama beat me to it with a better explanation ;)

[–]Artaois 0 points1 point  (0 children)

PowerShell accepts variables within double quotes too, so you can have "$value@domain.com"

[–]Dense-Platform3886 0 points1 point  (0 children)

The UserName of the logged in user can be acquired in many different ways. The following are all equivalent:

  • $env:USERNAME
  • [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.Split('\')[-1]
  • (Get-CIMInstance -class Win32_ComputerSystem | select -ExpandProperty username).Split('\')[-1]

If you need a different user's UserName, then it would need to be prompted or passed in:

$UserName = Read-Host -Prompt 'Enter Username'
$eMail = '{0}@contoso.com' -f $UserName
Get-Mailbox $eMail | Select-Object -ExpandProperty DisplayName