locked
Delete Profiles from Powershell RRS feed

  • Question

  • Hello,

    I am trying to write a script that will run on server 2008r2, 2012r2 and 2016. I want to delete domain profiles on each server that has not been used in over 6 months. Here is what i have:

    $users = Get-WmiObject -Class Win32_userProfile | Where{$_.LastUseTime -gt $(Get-Date).Date.AddDays(-180)} | where{$_.localpath -like ‘*.x'}

    Foreach($user in $users)

    {

    $user.Delete()

    }

    When I call the Delete function it tells me: Exception calling "Delete" With 0 Arguments

    What do I need to do to get this working?

    • Moved by Bill_Stewart Tuesday, December 11, 2018 8:51 PM Abandoned
    Wednesday, July 11, 2018 8:57 PM

All replies

  • Get-WmiObject -Class win32_userprofile | Where-Object {$_.LastUseTime -gt $(Get-Date).Date.AddDays(-180)} | where{$_.localpath -like ‘*.x'} | ForEach {$_.Delete()}
    
    give her a shot.
    Wednesday, July 11, 2018 9:08 PM
  • You are trying to delete profiles owned by the system.  They cannot and should not be deleted.

    Get-WmiObject win32_userprofile -Filter 'Special=False' | 
        Where-Object{
            -not $_.Loaded -and #skips loaded profiles
            $_.lastusetime -and # skips unused profiles
            $_.ConvertToDateTime($_.lastusetime) -gt [datetime]::Today.AddDays(-180) -and
            $_.localpath # skips blank paths
        } | 
        ForEach {$_.Delete()}

    You should also check the SID to be sure it is not a special domain or local account.


    \_(ツ)_/

    Wednesday, July 11, 2018 9:29 PM
  • Here is an even more targeted and filterable version.  The "-notmatch" clause can be added to.

    Get-WmiObject win32_userprofile -Filter 'Special=False' | 
        ForEach-Object{
            $SID = [System.Security.Principal.SecurityIdentifier]$_.SID
            $NtAccount = $SID.Translate([System.Security.Principal.NTAccount])
            $_ | Add-Member -MemberType NoteProperty -Name NtAccount -Value $NtAccount
            $_ | Add-Member -MemberType NoteProperty -Name Domain -Value ($NtAccount -split '\\')[0]
            $_ #pass the updated object
        } |
        Where-Object{
            #-not $_.Loaded -and #skips loaded profiles
            $_.lastusetime -and # skips unused profiles
            $_.Domain -notmatch 'NT SERVICE|LOCAL SERVICE' -and
            $_.ConvertToDateTime($_.lastusetime) -gt [datetime]::Today.AddDays(-180) -and
            $_.localpath # skips blank paths
        } |
        Select-Object Domain, NtAccount, LastUseTime, LocalPath

    Change "Select" to "ForEach-Object{ $_.Delete()}" to remove the profiles.


    \_(ツ)_/



    • Edited by jrv Thursday, July 12, 2018 1:00 AM
    Thursday, July 12, 2018 12:58 AM
  • The accounts that I want to delete all in .x so im filtering out any local or domain accounts that do not end in .x. I can get the list but I want to be able to delete the profile from the computer.
    Thursday, July 12, 2018 12:27 PM
  • This seemed to work thank you.
    Thursday, July 12, 2018 12:30 PM
  • This seemed to work thank you.

    What seemed to work?

    Note that the profile does not have to be named after the account.  There are many things that will cause a profile to be renamed by the system.


    \_(ツ)_/

    Thursday, July 12, 2018 12:45 PM
  • Get-WmiObject -Class win32_userprofile | Where-Object {$_.LastUseTime -gt $(Get-Date).Date.AddDays(-180)} | where{$_.localpath -like ‘*.x'} | ForEach {$_.Delete()}
    It deletes the profile, however it still tells me that it cant call "Delete" with 0 Arguments. Is there anyway to suppress this?
    Thursday, July 12, 2018 12:49 PM
  • Get-WmiObject -Class win32_userprofile | Where-Object {$_.LastUseTime -gt $(Get-Date).Date.AddDays(-180)} | where{$_.localpath -like ‘*.x'} | ForEach {$_.Delete()}
    It deletes the profile, however it still tells me that it cant call "Delete" with 0 Arguments. Is there anyway to suppress this?

    You have to understand why my code is written the way it has been written.  You cannot match a string with a date. "LastUseTime" is a string.


    \_(ツ)_/

    Thursday, July 12, 2018 12:52 PM