Warwick_Tech Posted October 3, 2022 Posted October 3, 2022 (edited) Hi all, Someone recently contacted me RE Delprof and I shared some alternative scripts with them, however I wanted to merge these scripts together; One works as it should, deletes all profiles - (well, except some weird ones for whatever reason, but 99%) $profiles = $null $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser"))} if ($profiles -ne $null) { $profiles | Remove-WmiObject } Anyway, the second script sort of works, but misses even more profiles, BUT it has the time stamp for selective profiles Get-CimInstance win32_userprofile -verbose | Where {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)} | Remove-CimInstance -Verbose So I thought I would combine them, but it doesn't work - anyone have any ideas? $profiles = $null $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and $_.LastUseTime -lt $(Get-Date).Date.AddDays(-14))} if ($profiles -ne $null) { $profiles | Remove-WmiObject } Edited October 3, 2022 by Warwick_Tech
howartp Posted October 3, 2022 Posted October 3, 2022 Don't forget the -verbose switch that's in your second script but not your final script.
howartp Posted October 3, 2022 Posted October 3, 2022 (Sorry, I might have typed too quick - I was thinking that was the equivalent of -properties * that is sometimes used to get extra properties, so my above comment may be pointless)
Warwick_Tech Posted October 4, 2022 Author Posted October 4, 2022 (Sorry, I might have typed too quick - I was thinking that was the equivalent of -properties * that is sometimes used to get extra properties, so my above comment may be pointless) No worries, I did try with the -verbose switches, but tbh I'm not sure what they did anyway. I have the feeling it is working as it just goes back to the > prompt, but I think the switch for adding days it's what's causing the issue.
browolf Posted October 6, 2022 Posted October 6, 2022 how does it not work? $profiles is null or "$profiles | Remove-WmiObject" doesn't work?
Warwick_Tech Posted October 7, 2022 Author Posted October 7, 2022 how does it not work? $profiles is null or "$profiles | Remove-WmiObject" doesn't work? Good question, it just quickly returns to the > without showing an error, but it doesn't work as no profiles are removed, even ones over the 14 day limit; remove the -14 days property and it will chug through and delete everything... Silly question is there a switch to show the output of the script so I can get more detailed info?
browolf Posted October 7, 2022 Posted October 7, 2022 (edited) Good question, it just quickly returns to the > without showing an error, but it doesn't work as no profiles are removed, even ones over the 14 day limit; remove the -14 days property and it will chug through and delete everything... Silly question is there a switch to show the output of the script so I can get more detailed info? I suggest print $profiles before trying to delete anything to check what is in that variable. also this looks to contain relevant information: https://stackoverflow.com/questions/70048684/remove-wmiobject-delete-user-profiles Edited October 7, 2022 by browolf 1
ThomL Posted October 7, 2022 Posted October 7, 2022 I haven't tested this, but there are some brackets missing on the $_.Lastusetime you combined into the Where. Does this work any better? $profiles = $null $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and ($_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)))} if ($profiles -ne $null) { $profiles | Remove-WmiObject } 1
Warwick_Tech Posted October 7, 2022 Author Posted October 7, 2022 I haven't tested this, but there are some brackets missing on the $_.Lastusetime you combined into the Where. Does this work any better? $profiles = $null $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser") -and ($_.LastUseTime -lt $(Get-Date).Date.AddDays(-14)))} if ($profiles -ne $null) { $profiles | Remove-WmiObject } Still no joy - - - Updated - - - I suggest print $profiles before trying to delete anything to check what is in that variable. also this looks to contain relevant information: https://stackoverflow.com/questions/70048684/remove-wmiobject-delete-user-profiles Just running print $profiles returns: No file to print Though that's the same on my PC...
ThomL Posted October 7, 2022 Posted October 7, 2022 The $_.lastusetime is looking for profile used in the last 2 weeks, is this what you're looking for? Or are you looking for profiles that haven't been used in the last 2 weeks?
Warwick_Tech Posted October 7, 2022 Author Posted October 7, 2022 The $_.lastusetime is looking for profile used in the last 2 weeks, is this what you're looking for? Or are you looking for profiles that haven't been used in the last 2 weeks? Oh - Yes it should be profiles NOT used in the last 2 weeks but to be fair, it's not even removing those
browolf Posted October 7, 2022 Posted October 7, 2022 Sorry, write-output is the command to print variables https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-7.2
ThomL Posted October 7, 2022 Posted October 7, 2022 (edited) I may have been wrong acting weird on my pc.... Quick edit, looks like it was my PC playing up! -lt is the correct operator Edited October 7, 2022 by ThomL
ThomL Posted October 7, 2022 Posted October 7, 2022 Does this output the correct list of profiles? $profiles = $null $delDate = (Get-Date).AddDays(-14) $persistentProfilePaths = "C:\Users\Administrator","C:\Users\UpdatusUser" $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LastUseTime -lt $delDate))} foreach($profile in $profiles | where {$persistentProfilePaths -notcontains $_.LocalPath}) { write-host "$($profile.LastUseTime) $($profile.LocalPath)" #$profiles | Remove-WmiObject }
Warwick_Tech Posted October 7, 2022 Author Posted October 7, 2022 Does this output the correct list of profiles? $profiles = $null $delDate = (Get-Date).AddDays(-14) $persistentProfilePaths = "C:\Users\Administrator","C:\Users\UpdatusUser" $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LastUseTime -lt $delDate))} foreach($profile in $profiles | where {$persistentProfilePaths -notcontains $_.LocalPath}) { write-host "$($profile.LastUseTime) $($profile.LocalPath)" #$profiles | Remove-WmiObject } No, just takes me back to > I think there's more going on here, as I can't seem to get it to recognise these are even profiles... what makes less sense, is as I say the base wipes all of them... $profiles = $null $profiles = Get-WMIObject -class Win32_UserProfile | Where {((!$_.Special) -and ($_.LocalPath -ne "C:\Users\Administrator") -and ($_.LocalPath -ne "C:\Users\UpdatusUser"))} if ($profiles -ne $null) { $profiles | Remove-WmiObject } Exit
browolf Posted October 7, 2022 Posted October 7, 2022 I suggest using windows terminal as it's a lot nicer than the built in ide. With terminal you can run ps commands on the command line and it's colour coded. When you're in this situation it's helpful to deconstruct the script as much as possible to pinpoint the problem. On the commandline you can just type a variable name to get it's contents e.g PS D:\>$profiles https://apps.microsoft.com/store/detail/windows-terminal/9N0DX20HK701?hl=en-gb&gl=
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now