troubleshooting Question

Unexpected token $_ in Powershell command

Avatar of matedwards
matedwardsFlag for United Kingdom of Great Britain and Northern Ireland asked on
Active DirectoryPowershell
4 Comments1 Solution207 ViewsLast Modified:
I'm trying to bulk modify AD users UPN suffix with a Powershell script (below).

Import-Module ActiveDirectory
$oldSuffix = "canitpro.local"
$newSuffix = ""
$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
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros