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?
Or, if you want to drop that into a variable,
[string]$domain = (get-addomain).dnsroot

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)"
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

Ben HartAuthor Commented:
Not sure this line would format an address like samaccountname-domain-com@domain.mail.onmicrosoft.com
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:

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?)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.