Script Block looks like it works but doesnt

Apologies if this shows up elsewhere, I looked through the first three or so pages and didn’t see anything on it.

Overall I am trying to delete user profiles from several terminal servers when a user leaves via the remprof.exe command.

Full disclosure: I want to have a CSV that lists all of our term servers (for ease of editing) and a separate CSV that lists the users (haven’t even gotten that far).

Testing out just the piece that deletes the profile sans variables, I am already failing. If I try just the invoke command:

invoke-command -ComputerName computerName -ScriptBlock {'C:\windows\system32\remprof.exe userAccountName'}

it looks like it works in the PS window, but doesn’t actually run the script on the remote computer. I can run literally the exact line ‘C:\windows\system32\remprof.exe userAccountName’ and it works fine.

From there it gets progressively worse with the foreach and get-content not working; powershell complaining the computer name isn’t valid

$computers = get-content "term_servers.csv"


foreach ($computer in $computers)
{

    $adcompname = Get-ADComputer $computer
    
    invoke-command -ComputerName $adcompname -ScriptBlock {'C:\windows\system32\remprof.exe userAccountName'}

    write-host "$computer done."

}

Any thoughts would be much appreciated!!

Thanks,
TJ

Keep in mind that a Remoting connection does not spin up a profile on the remote machine, and it runs under delegated authentication. It’s possible that RemProf.exe simply won’t work in that context.

if you are trying to remove local profiles you might look at something Boe Prox (https://twitter.com/proxb) wrote a while ago:

http://poshcode.org/2477

I used it when I needed to manage profiles with Remote desktop servers.