Powershell script help - How to combine values

Long story short, I'm crafting a script to create a new domain user.. part of my script adds values like Description, and proxy address.. but to help us add the correct proxyaddress that will work with our O365 tenant I need to combine teh username with a custom string and I'm hitting a brick wall with the syntax.

Here's what I have:

$theOU = read-host "Enter the OU name"
$Surname = read-Host "Enter the surname"
$GivenName = read-host "Enter first name"
$DisplayName = "$Surname, $GivenName"
$Password = "December1"
$name = $GivenName.substring(0,1)+$Surname
$proxyaddress = read-host "Enter the proxy address in full"
#$txtBoxTelephone = read-host "Enter phone #"
$DomainProxyAddress = $GivenName.substring(0,1)+$Surname <------------------------------------
$txtBoxDescription = read-host "Enter persons description"
Import-Module activedirectory
import-module servermanager

	$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=domain,DC=org'
Get-ADuser  -filter * -Properties ProxyAddresses|?{$_.proxyaddresses -contains $proxyaddress}
	$found=Get-ADuser  -filter * -Properties ProxyAddresses|
        $_.proxyaddresses | 
             Where-Object{ $_ -eq $ProxyAddress }}

	write-host "ProxyAddress Exists, Change username to something unique!"
get-aduser -filter * -properties $proxyaddress
pause 5
	else {
	write-host "ProxyAddress Not Found!"
Pause 5


write-host	$myou
write-host $name
New-ADUser -path $myOU -samaccountname $name -name $displayname -DisplayName $DisplayName -Surname $Surname -givenname $givenname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -force) -enabled:$false
set-aduser $name -properties proxyaddresses $proxyaddress
set-aduser $name -properties proxyaddresses 	
get-aduser $name

Open in new window

$DomainProxyAddress is the line I'm editing currently.  I basically need a secondary proxy address like:

Tester, Adam

I do not know the correct syntax to create this string... Help?
LVL 14
Ben HartAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ben HartAuthor Commented:
Ok I have to correct myself a little here..  The Correct syntax for adding the proxyaddress is:

set-aduser $name -add @{proxyaddresses = "$proxyaddress"}

Open in new window

So now I just need to determine how to format it in the way I explained above...
Try this:
$DomainProxyAddress = "$($GivenName.substring(0,1))$($Surname)"
Ben HartAuthor Commented:
Not sure this line would format an address like samaccountname-domain-com@domain.mail.onmicrosoft.com
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Perhaps I misunderstood.
$givenname.substring(0,1) gives you the first bit,
$surname gives you the latter part.
Where are you obtaining domain-com and @domain.mail.onmicrosoft.com (presumably this is mostly hard-set?)
Ben HartAuthor Commented:
Getting the first initial and last name or in my case the SAM i can already do.  It's tying that into the @domain.mail.blah blah that's throwing me off.

Yeah It'd be a static variable so probably defined as another $proxyaddress
Sort of convaluted, but this retrieves the info from my domain.
Though I don't know that I quite fully understand your setup enough to know if it will work for you:

Or, if you want to drop that into a variable,
[string]$domain = (get-addomain).dnsroot


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ben HartAuthor Commented:
That's not our domain per say, but a secondary proxy address for Office365.
Then I'm afraid I don't quite understand.
Where would you gather the domain information and would it be the same for both instances of 'domain' above?
Ben HartAuthor Commented:
So if my proxy domain is Widgets.mail.onmicrosoft.com then judging by your string above I could change it to:

Ben HartAuthor Commented:
The proxy domain is a static entry in the script.  It wouldnt be 'pulled' from anywhere like AD.
Yes, the above string should do it, if you can get your domain into the $domain variable.
Ben HartAuthor Commented:
BAM, got it!  With your help I modified your string to:

$DomainProxyAddress = "$($givenname.substring(0,1))$surname-$("mydomain")-$("net")@domain.mail.onmicrosoft.com"
Only the methods need to be delayed by enclosing them in $(), you shouldn't have to do that with the string - use this instead:

$DomainProxyAddress = "$($givenname.substring(0,1))$surname-mydomain-net@domain.mail.onmicrosoft.com"

(of course, that last 'domain' isn't accounted for yet?)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.