Unexpected token $_ in Powershell command

I'm trying to bulk modify AD users UPN suffix with a Powershell script (below).

Import-Module ActiveDirectory
$oldSuffix = "canitpro.local"
$newSuffix = "rebeladmin.com"
$ou = "DC=canitpro,DC=local"
$server = "DCM1"
Get-ADUser -SearchBase $ou -filter * | ForEach-Object {
$newUpn = $_.UserPrincipalName.Replace($oldSuffix,$newSuffix)
$_ | Set-ADUser -server $server -UserPrincipalName $newUpn
}

But I keep getting the error: Unexpected token '$_' in expression or statement

What is wrong with the $_ ?

I thought it represented every object in the pipeline?

Many thanks for any help
matedwardsAsked:
Who is Participating?
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.

oBdACommented:
How are you trying to run this? Directly pasted into a PS console, or from a script?
That should work just fine if you copy and paste it back from here.
Make sure that inside the loop, you have either a proper line break between the two lines, or a ";".
Make sure as well that the first line inside the loop doesn't end with a backtick.
And you should use the -replace statement, not the Replace() method - the latter is case sensitive, so if the old suffix for a user contains any capital character, it won't be replaced.
Import-Module ActiveDirectory
$oldSuffix = "canitpro.local"
$newSuffix = "rebeladmin.com"
$ou = "DC=canitpro,DC=local"
$server = "DCM1"
Get-ADUser -SearchBase $ou -filter * | ForEach-Object {
	$newUpn = $_.UserPrincipalName -replace $oldSuffix, $newSuffix
	$_ | Set-ADUser -server $server -UserPrincipalName $newUpn
}

Open in new window

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
matedwardsAuthor Commented:
Many thanks oBdA,

I'm trying to run it direct in the console.
I will cut and paste again from here and see.
Will update you soon.

Many thanks again
matedwardsAuthor Commented:
Thanks oBdA, worked a treat.

Being a newbie to powershell can I ask why I should use the -replace statement not the Replace() method?

Is it solely down to being case sensitive?

Many thanks again
oBdACommented:
In this case because it's case insensitive.
As an alternative, you can make the string all lower before replacing, but that would include the part not to be replaced; that might not be what you want:
	$newUpn = $_.UserPrincipalName.ToLower().Replace($oldSuffix, $newSuffix)

Open in new window

Note that the -replace operator is actually way more powerful than the Replace() method - it uses a Regular Expression for the pattern to replace.
So don't go replacing the method with the operator all over the place before reading up on Regular Expressions.
To be completely correct, the "." in the name would need to be escaped with a backslash (or using [regex]::Escape()) since the "." means "any character" in RegEx, but in this case, it's rather safe to leave it in.
	$newUpn = $_.UserPrincipalName -replace ([regex]::Escape($oldSuffix)), $newSuffix

Open in new window

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
Powershell

From novice to tech pro — start learning today.