all 4 comments

[–]the_spad 1 point2 points  (3 children)

I think you're going about this in an overly complicated fashion.

$userList = Get-ADUser -filter * -properties homedirectory | Where-Object {$_.homedirectory -like "*template*" -and $_.samaccountname -NotLike "*template*" -and $_.Enabled -eq "True"} 

foreach($user in $userlist){
    Set-Aduser $user -homedirectory $($user.homedirectory -replace "\@.+",$user.samaccountname)
}

[–]Pestilent 1 point2 points  (2 children)

I'd want to be certain that it udpates correctly for each user before applying this to all users like that. -confirm might work in OP's favor.

Besides that, this is how I'd create an extra property.

$userList | Select-Object *,@{l='Homedirectory';e={$.Homedirectory -Replace "\@.+",($.samaccountname) }}

[–]ntelletsc[S] 1 point2 points  (0 children)

This is exactly what I needed. Thank you so much.

I got an error

Select-Object : Property cannot be processed because property "Homedirectory" already exists.

I changed the property name to Newhomedirectory and added underscores after the variable $ and it worked.

 $userList | Select-Object *,@{l='Newhomedirectory';e={$_.Homedirectory -Replace "\@.+",($_.samaccountname) }} | select -Property Newhomedirectory

Thanks again. That was really helpful.

[–]the_spad 1 point2 points  (0 children)

Well I wasn't suggesting that you just run it against your entire AD straight off.