Deleting Profiles Remotely

Welcome Forums General PowerShell Q&A Deleting Profiles Remotely

Viewing 11 reply threads
  • Author
    Posts
    • #207540
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      Hi…. Looking for a script to delete all profiles remotely

      We want to clear all profiles so the machine can be used for another user. The machines will be offsite

      Very new to powershell so any help would be great

      Thanks

      Dan

    • #207546
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,985
      Helping Hand
      Rank: Community Hero

      Have you searched for “Delete Profiles Powershell”?

      https://www.business.com/articles/powershell-manage-user-profiles/

    • #207561
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      The Script is just for local use, Need to delete from a networked machine

    • #207585
      js
      Participant
      Topics: 32
      Replies: 852
      Points: 2,779
      Helping Hand
      Rank: Community Hero

      You can remove wmi win32_userprofile objects. There’s a group policy to do it too.

    • #207588
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      Thanks for the answer is there a full script I could copy

    • #207636
      Participant
      Topics: 10
      Replies: 200
      Points: 982
      Helping Hand
      Rank: Major Contributor

      The link Rob referenced has your answer:

      Get-WmiObject -Class Win32_UserProfile ComputerName CLIENT1,CLIENT2 | where {$_.LocalPath.split('')[-1] -eq 'Administrator.CLIENT1'} | foreach {$_.Delete()}

      To delete all profiles, you will want to modify to something like this so you DONT delete the System Profiles:

      Get-WmiObject -ClassName Win32_UserProfile ComputerName CLIENT1,CLIENT2 | Where-Object {!($_.LocalPath -Match 'windows')} | foreach {$_.Delete()}

      I am thinking that nuking the system profiles might be a bad idea.

      You can view all profiles as such:

      (Get-WmiObject -ClassName Win32_UserProfile ComputerName COMPUTER1).LocalPath

       

      • This reply was modified 10 months, 2 weeks ago by TonyD.
      • This reply was modified 10 months, 2 weeks ago by TonyD.
    • #207723
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,437
      Helping Hand
      Rank: Community MVP

      Did you try to search for the topic before you started asking?

      https://adamtheautomator.com/powershell-delete-user-profile/

    • #207831
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      Thank you, Script ran without any errors but didn’t delete the profile from c:\

    • #207837
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      $Computer = Read-Host “Please Enter Computer Name: ”
      $user = Read-Host “Enter User ID: ”

      Invoke-Command -ComputerName $computer -ScriptBlock {
      param($user)
      $localpath = ‘c:\users\’ + $user
      Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |
      Remove-WmiObject
      } -ArgumentList $user

       

      This seems to work on local machines but not remote any ideas?

    • #207873
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      I have got this script to work removing 1 profile is there anyway to put exclusions in it so we can wipe all profiles except the built in accounts for example public

      $Computer = Read-Host “Please Enter Computer Name: ”
      $user = Read-Host “Enter User ID: ”

      Invoke-Command -ComputerName “” -ScriptBlock {
      param($user)
      $localpath = ‘c:\users\’ + “”
      Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -eq $localpath} |
      Remove-WmiObject
      } -ArgumentList $user

    • #207900
      Participant
      Topics: 5
      Replies: 17
      Points: 66
      Rank: Member

      So this is nearly exactly what I need, How would I add an option for all profiles rather than deleting 1 by 1

       

      #Prompt for a computer to connect to
      $computer = Read-Host “Please enter a computer name”
      #Test network connection before making connection
      If ($computer -ne $Env:Computername) {
      If (!(Test-Connection -comp $computer -count 1 -quiet)) {
      Write-Warning “$computer is not accessible, please try a different computer or verify it is powered on.”
      Break
      }
      }
      Try {
      # #Verify that the OS Version is 6.0 and above, otherwise the script will fail
      # If ((Get-WmiObject -ComputerName $computer Win32_OperatingSystem -ea stop).Version -lt 6.0) {
      # Write-Warning “The Operating System of the computer is not supported. nClient: Vista and abovenServer: Windows 2008 and above.”
      # Break
      # }
      }
      Catch {
      Write-Warning “$($error[0])”
      Break
      }
      Do {
      #Gather all of the user profiles on computer
      Try {
      [array]$users = Get-WmiObject -ComputerName $computer Win32_UserProfile -filter “LocalPath Like ‘C:\\Users\\%'” -ea stop
      }
      Catch {
      Write-Warning “$($error[0]) ”
      Break
      }
      #Cache the number of users
      $num_users = $users.count

      Write-Host -ForegroundColor Green “User profiles on $($computer):”

      #Begin iterating through all of the accounts to display
      For ($i=0;$i -lt $num_users; $i++) {
      Write-Host -ForegroundColor Green “$($i): $(($users[$i].localpath).replace(‘C:\Users\’,”))”
      }
      Write-Host -ForegroundColor Green “q: Quit”
      #Prompt for user to select a profile to remove from computer
      Do {
      $account = Read-Host “Select a number to delete local profile or ‘q’ to quit”
      #Find out if user selected to quit, otherwise answer is an integer
      If ($account -NotLike “q*”) {
      $account = $account -as [int]
      }
      }
      #Ensure that the selection is a number and within the valid range
      Until (($account -lt $num_users -AND $account -match “\d”) -OR $account -Like “q*”)
      If ($account -Like “q*”) {
      Break
      }
      Write-Host -ForegroundColor Yellow “Deleting profile: $(($users[$account].localpath).replace(‘C:\Users\’,”))”
      #Remove the local profile
      ($users[$account]).Delete()
      Write-Host -ForegroundColor Green “Profile: $(($users[$account].localpath).replace(‘C:\Users\’,”)) has been deleted”

      #Configure yes choice
      $yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”,”Remove another profile.”

      #Configure no choice
      $no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”,”Quit profile removal”

      #Determine Values for Choice
      $choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes,$no)

      #Determine Default Selection
      [int]$default = 0

      #Present choice option to user
      $userchoice = $host.ui.PromptforChoice(“”,”Remove Another Profile?”,$choice,$default)
      }
      #If user selects No, then quit the script
      Until ($userchoice -eq 1)

    • #207930
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,985
      Helping Hand
      Rank: Community Hero

      The filtering can be done more efficiently. First, let’s look at all properities:

      Next, cleanup the output to see what are relevant filters can be used:

      PS C:\Users\rasim> Get-CimInstance -ClassName Win32_UserProfile | Select LocalPath, Special, Loaded, RoamingConfigured

      The output shows the Special or system profiles. Next, you probably should not be deleting profiles that are loaded, being utilized currently. Lastly, you specified it has to be local path, so you want to ensure that it is not a Roaming profile:

      As mentioned above, I would also filter Loaded (e.g. “and Loaded = ‘False'”), but I am using the profile. As you can see everything is filtered and now we can use a calculated expression to just get the profile name. The [-1] represents the UpperBound value, the highest part of the array. To answer your last question,

      Found the index stuff here, but looks similar to how you were referencing the item to delete in your above post:

      Finding the index of an object within an array by property value using PowerShell

Viewing 11 reply threads
  • The topic ‘Deleting Profiles Remotely’ is closed to new replies.