Hi,
You can use $using:user
to identify the local variable $user
About Remote Variables
foreach($computer in $computers){
foreach($user in $users){ Invoke-Command -ComputerName $computer -ScriptBlock {
$localpath = 'c:\users\' + $using:user
write-output "Removing profile $localpath"
Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} | Remove-WmiObject
}
}
}
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.