Unexpected token $_ in Powershell command

matedwards
matedwards used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Author

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

Author

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
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial