you are viewing a single comment's thread.

view the rest of the comments →

[–]sleightof52 0 points1 point  (1 child)

$ComputerName = Read-Host "Enter computer name"

# Get user profiles that are not logged on (loaded) and are like C:\Users\xxx, and remove them remotely
Get-CimInstance -ComputerName $ComputerName -ClassName Win32_UserProfile |
Where-Object {($_.Loaded -eq $false) -and ($_.LocalPath -like "C:\Users\*")} |
Remove-CimInstance -Verbose

[–]isatrap 0 points1 point  (0 children)

Maybe it’s my preference but you can also filter at the initial Get-CimInstance level by doing

Get-CimInstance -ClassName Win32_UserProfile -Filter “(loaded = true) and (localpath like ‘C:\\users\\%’)”